Last modified: Mon Mar 02 2020 07:15:13 GMT+0000 (Coordinated Universal Time)
Mongoose Model Methods
find(criteria, [fields], [options], [callback])
: find document; callback has error and documents argumentscount(criteria, [callback]))
: return a count; callback has error and count argumentsfindById(id, [fields], [options], [callback])
: return a single document by ID; callback has error and document argumentsfindByIdAndUpdate(id, [update], [options], [callback])
: executes MongoDB's findAndModify to update by IDfindByIdAndRemove(id, [options], [callback])
: executes MongoDB's findAndModify to removefindOne(criteria, [fields], [options], [callback])
: return a single document; callback has error and document argumentsfindOneAndUpdate([criteria], [update], [options], [callback])
: executes MongoDB's findAndModify to updatefindOneAndRemove(id, [update], [options], [callback])
: executes MongoDB's findAndModify to removeupdate(criteria, update, [options], [callback])
: update documents; callback has error, and count argumentscreate(doc(s), [callback])
: create document object and save it to database; callback has error and doc(s) argumentsremove(criteria, [callback])
: remove documents; callback has error argument
Mongoose Document Methods
save([callback])
: save the document; callback has error, doc and count argumentsset(path, val, [type], [options])
: set value on the doc's propertyget(path, [type])
: get the valueisModified([path])
: check if the property has been modifiedpopulate([path], [callback])
: populate referencetoJSON(options)
: get JSON from documentvalidate(callback)
: validate the document
Query Helpers
animalSchema.query.byName = function(name) {
return this.where({ name: new RegExp(name, "i") })
}
var Animal = mongoose.model("Animal", animalSchema)
Animal.find()
.byName("fido")
.exec(function(err, animals) {
console.log(animals)
})
Animal.findOne()
.byName("fido")
.exec(function(err, animal) {
console.log(animal)
})