Mongoose static methods:
- Model.create(data)
- Model.insertMany(arr)
- Model.find()
- find all documents in a collection
- note: returns an array (even if there's zero or one documents)
- Model.find(filter)
- find all documents that match the filter.
- ex:
Model.find({price: {$gt: 20} })
- Model.findOne(filter)
- Model.findById(id)
- Model.updateMany(filter, update [, options])
- Model.findOneAndUpdate(filter, update [, options])
- Model.findByIdAndUpdate(id, update [, options])
- By default, it 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 })
- Model.deleteMany(filter)
- Model.deleteOne(filter)
- Model.findByIdAndDelete(id)