Mongoose static methods:
- Model.create(data)
- Model.insertMany(arr)
- Model.find(filter)
- If the filter is not provided or an empty object: it returns all the documents in a collection (example:
Pizza.find()
) - If the filter is provided: it returns all the documents in meet that criteria (example:
Pizza.find({price: {$gt: 20} })
) - note: it always returns an array (even if there's zero or one documents)
- If the filter is not provided or an empty object: it returns all the documents in a collection (example:
- Model.findOne(filter)
- Model.findById(id)
- Model.updateMany(filter, update [, options])
- Model.findOneAndUpdate(filter, update [, options])
- Model.findByIdAndUpdate(id, update [, options])
- Note 1: by default, Mongoose will return the document before the update was applied.
- In case you want to receive the updated document, you can pass a third argument with
{ new: true }
. - Example:
Model.findByIdAndUpdate(id, update, { new: true })
- In case you want to receive the updated document, you can pass a third argument with
- Note 2: by default, Mongoose does not run validation on updates.
- This can be configured on individual updates with this syntax:
Model.findByIdAndUpdate(id, update, { new: true, runValidators: true })
- or, it can be turned on for all updates, adding this line:
mongoose.set('runValidators', true);
- This can be configured on individual updates with this syntax:
- Note 1: by default, Mongoose will return the document before the update was applied.
- Model.deleteMany(filter)
- Model.deleteOne(filter)
- Model.findByIdAndDelete(id)