# Introduction to Mongoose Documents

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published May 8, 2022. Topics: Node.js, Mongoose, MongoDB.
Canonical URL: https://attacomsian.com/blog/mongoose-documents

> An introduction to Mongoose documents and how to use Mongoose model functions to query, update, and delete documents from a MongoDB collection.

Mongoose documents represent a one-to-one mapping to documents stored in a [MongoDB collection](https://attacomsian.com/blog/working-with-mongodb-using-mongo-shell#create-a-collection). Every document is an instance of its [model](https://attacomsian.com/blog/mongoose-models).

## Documents vs. models

In Mongoose, both `Document` and `Model` are two separate classes. The `Model` class is a subclass of the `Document` class. When you call the `Model` class constructor, you actually create a new document:

```javascript
const mongoose = require('mongoose')
const { Schema } = mongoose

const Product = mongoose.model(
  'Product',
  new Schema({
    name: String,
    price: Number
  })
)

const prod = new Product()

prod instanceof mongoose.Model // true
prod instanceof mongoose.Document // true
```

## Querying documents

You can retrieve a Mongoose document from the MongoDB using model functions like `findOne()`:

```javascript
const product = await Product.findOne({ name: 'Pizza' })
```

To retrieve multiple documents, use the `find()` method instead:

```javascript
const products = await Product.find({ price: { $gt: 10 } })
```

## Updating documents

All the changes made to a Mongoose document are tracked. You can modify a document just like a JavaScript object, and Mongoose will automatically convert it into a MongoDB update query:

```javascript
const pizza = await Product.findOne({ name: 'Pizza' })
pizza.price = 10

await pizza.save()
```

To update more than one document at once, you should use the `updateMany()` function:

```javascript
await Product.updateMany({ price: { $lte: 5 } }, { $set: { price: 10 } })
```

## Deleting documents

Mongoose models have static `deleteOne()` and `deleteMany()` functions for removing all documents matching the given filters:

```javascript
// Delete one document
await Product.deleteOne({ name: 'Pizza' })

// Delete multiple documents
await Product.deleteMany({ price: { $lt: 5 } })
```
