A Model is a class that's your primary tool for interacting with MongoDB. An instance of a Model is called a Document.
In Mongoose, the term "Model" refers to subclasses of the mongoose.Model
class. You should not use the mongoose.Model
class directly. The mongoose.model()
and connection.model()
functions create subclasses of mongoose.Model
as shown below.
// `UserModel` is a "Model", a subclass of `mongoose.Model`.
const UserModel = mongoose.model('User', new Schema({ name: String }));
// You can use a Model to create new documents using `new`:
const userDoc = new UserModel({ name: 'Foo' });
await userDoc.save();
// You also use a model to create queries:
const userFromDb = await UserModel.findOne({ name: 'Foo' });
Performs aggregations on the models collection.
If a callback
is passed, the aggregate
is executed and a Promise
is returned. If a callback is not passed, the aggregate
itself is returned.
This function triggers the following middleware.
aggregate()
// Find the max balance of all accounts
Users.aggregate([
{ $group: { _id: null, maxBalance: { $max: '$balance' }}},
{ $project: { _id: 0, maxBalance: 1 }}
]).
then(function (res) {
console.log(res); // [ { maxBalance: 98000 } ]
});
// Or use the aggregation pipeline builder.
Users.aggregate().
group({ _id: null, maxBalance: { $max: '$balance' } }).
project('-id maxBalance').
exec(function (err, res) {
if (err) return handleError(err);
console.log(res); // [ { maxBalance: 98 } ]
});
$project
and $group
operators allow redefining the "shape" of the documents at any stage of the pipeline, which may leave documents in an incompatible format. You can use the mongoose-cast-aggregation plugin to enable minimal casting for aggregation pipelines.update
filter
update
filter
Query#w()
for more information. function(error, bulkWriteOpResult) {}
BulkWriteOpResult
if the operation succeeds Sends multiple insertOne
, updateOne
, updateMany
, replaceOne
, deleteOne
, and/or deleteMany
operations to the MongoDB server in one command. This is faster than sending multiple independent operations (like) if you use create()
) because with bulkWrite()
there is only one round trip to MongoDB.
Mongoose will perform casting on all operations you provide.
This function does not trigger any middleware, not save()
nor update()
. If you need to trigger save()
middleware for every document use create()
instead.
Character.bulkWrite([
{
insertOne: {
document: {
name: 'Eddard Stark',
title: 'Warden of the North'
}
}
},
{
updateOne: {
filter: { name: 'Eddard Stark' },
// If you were using the MongoDB driver directly, you'd need to do
// `update: { $set: { title: ... } }` but mongoose adds $set for
// you.
update: { title: 'Hand of the King' }
}
},
{
deleteOne: {
{
filter: { name: 'Eddard Stark' }
}
}
}
]).then(res => {
// Prints "1 1 1"
console.log(res.insertedCount, res.modifiedCount, res.deletedCount);
});
The supported operations are:
insertOne
updateOne
updateMany
deleteOne
deleteMany
replaceOne
undefined
if callback is specified, returns a promise if no callback. Deletes all indexes that aren't defined in this model's schema. Used by syncIndexes()
.
The returned promise resolves to a list of the dropped indexes' names as an array
Counts number of documents that match filter
in a database collection.
This method is deprecated. If you want to count the number of documents in a collection, e.g. count({})
, use the estimatedDocumentCount()
function instead. Otherwise, use the countDocuments()
function instead.
Adventure.count({ type: 'jungle' }, function (err, count) {
if (err) ..
console.log('there are %d jungle adventures', count);
});
Counts number of documents matching filter
in a database collection.
Adventure.countDocuments({ type: 'jungle' }, function (err, count) {
console.log('there are %d jungle adventures', count);
});
If you want to count all documents in a large collection, use the estimatedDocumentCount()
function instead. If you call countDocuments({})
, MongoDB will always execute a full collection scan and not use any indexes.
The countDocuments()
function is similar to count()
, but there are a few operators that countDocuments()
does not support. Below are the operators that count()
supports but countDocuments()
does not, and the suggested replacement:
$where
: $expr
$near
: $geoWithin
with $center
$nearSphere
: $geoWithin
with $centerSphere
save()
. To specify options
, docs
must be an array, not a spread. Shortcut for saving one or more documents to the database. MyModel.create(docs)
does new MyModel(doc).save()
for every doc in docs.
This function triggers the following middleware.
save()
// pass a spread of docs and a callback
Candy.create({ type: 'jelly bean' }, { type: 'snickers' }, function (err, jellybean, snickers) {
if (err) // ...
});
// pass an array of docs
var array = [{ type: 'jelly bean' }, { type: 'snickers' }];
Candy.create(array, function (err, candies) {
if (err) // ...
var jellybean = candies[0];
var snickers = candies[1];
// ...
});
// callback is optional; use the returned promise if you like:
var promise = Candy.create({ type: 'jawbreaker' });
promise.then(function (jawbreaker) {
// ...
})
Create the collection for this model. By default, if no indexes are specified, mongoose will not create the collection for the model until any documents are created. Use this method to create the collection explicitly.
Note 1: You may need to call this before starting a transaction See https://docs.mongodb.com/manual/core/transactions/#transactions-and-operations
Note 2: You don't have to call this if your schema contains index or unique field. In that case, just use Model.init()
var userSchema = new Schema({ name: String })
var User = mongoose.model('User', userSchema);
User.createCollection().then(function(collection) {
console.log('Collection is created!');
});
Similar to ensureIndexes()
, except for it uses the createIndex
function.
Query.prototype.setOptions()
Deletes all of the documents that match conditions
from the collection. Behaves like remove()
, but deletes all documents that match conditions
regardless of the single
option.
Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, function (err) {});
Like Model.remove()
, this function does not trigger pre('remove')
or post('remove')
hooks.
Query.prototype.setOptions()
Deletes the first document that matches conditions
from the collection. Behaves like remove()
, but deletes at most one document regardless of the single
option.
Character.deleteOne({ name: 'Eddard Stark' }, function (err) {});
Like Model.remove()
, this function does not trigger pre('remove')
or post('remove')
hooks.
discriminatorKey
property. If not specified, Mongoose uses the name
parameter. Adds a discriminator type.
function BaseSchema() {
Schema.apply(this, arguments);
this.add({
name: String,
createdAt: Date
});
}
util.inherits(BaseSchema, Schema);
var PersonSchema = new BaseSchema();
var BossSchema = new BaseSchema({ department: String });
var Person = mongoose.model('Person', PersonSchema);
var Boss = Person.discriminator('Boss', BossSchema);
new Boss().__t; // "Boss". `__t` is the default `discriminatorKey`
var employeeSchema = new Schema({ boss: ObjectId });
var Employee = Person.discriminator('Employee', employeeSchema, 'staff');
new Employee().__t; // "staff" because of 3rd argument above
Creates a Query for a distinct
operation.
Passing a callback
executes the query.
Link.distinct('url', { clicks: {$gt: 100}}, function (err, result) {
if (err) return handleError(err);
assert(Array.isArray(result));
console.log('unique urls with more than 100 clicks', result);
})
var query = Link.distinct('url');
query.exec(callback);
Sends createIndex
commands to mongo for each index declared in the schema. The createIndex
commands are sent in series.
Event.ensureIndexes(function (err) {
if (err) return handleError(err);
});
After completion, an index
event is emitted on this Model
passing an error if one occurred.
var eventSchema = new Schema({ thing: { type: 'string', unique: true }})
var Event = mongoose.model('Event', eventSchema);
Event.on('index', function (err) {
if (err) console.error(err); // error occurred during index creation
})
NOTE: It is not recommended that you run this in production. Index creation may impact database performance depending on your load. Use with caution.
Estimates the number of documents in the MongoDB collection. Faster than using countDocuments()
for large collections because estimatedDocumentCount()
uses collection metadata rather than scanning the entire collection.
const numAdventures = Adventure.estimatedDocumentCount();
Event emitter that reports any errors that occurred. Useful for global error handling.
MyModel.events.on('error', err => console.log(err.message));
// Prints a 'CastError' because of the above handler
await MyModel.findOne({ _id: 'notanid' }).catch(noop);
Returns true if at least one document exists in the database that matches the given filter
, and false otherwise.
Under the hood, MyModel.exists({ answer: 42 })
is equivalent to MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean().then(doc => !!doc)
await Character.deleteMany({});
await Character.create({ name: 'Jean-Luc Picard' });
await Character.exists({ name: /picard/i }); // true
await Character.exists({ name: /riker/i }); // false
This function triggers the following middleware.
findOne()
Query.prototype.select()
Query.prototype.setOptions()
Finds documents.
The filter
are cast to their respective SchemaTypes before the command is sent. See our query casting tutorial for more information on how Mongoose casts filter
.
// named john and at least 18
MyModel.find({ name: 'john', age: { $gte: 18 }});
// executes, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});
// executes, name LIKE john and only selecting the "name" and "friends" fields
MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })
// passing options
MyModel.find({ name: /john/i }, null, { skip: 10 })
// passing options and executes
MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {});
// executing a query explicitly
var query = MyModel.find({ name: /john/i }, null, { skip: 10 })
query.exec(function (err, docs) {});
// using the promise returned from executing a query
var query = MyModel.find({ name: /john/i }, null, { skip: 10 });
var promise = query.exec();
promise.addBack(function (err, docs) {});
_id
to query by Query.prototype.select()
Query.prototype.setOptions()
Finds a single document by its _id field. findById(id)
is almost* equivalent to findOne({ _id: id })
. If you want to query by a document's _id
, use findById()
instead of findOne()
.
The id
is cast based on the Schema before sending the command.
This function triggers the following middleware.
findOne()
* Except for how it treats undefined
. If you use findOne()
, you'll see that findOne(undefined)
and findOne({ _id: undefined })
are equivalent to findOne({})
and return arbitrary documents. However, mongoose translates findById(undefined)
into findOne({ _id: null })
.
// find adventure by id and execute
Adventure.findById(id, function (err, adventure) {});
// same as above
Adventure.findById(id).exec(callback);
// select only the adventures name and length
Adventure.findById(id, 'name length', function (err, adventure) {});
// same as above
Adventure.findById(id, 'name length').exec(callback);
// include all properties except for `length`
Adventure.findById(id, '-length').exec(function (err, adventure) {});
// passing options (in this case return the raw js objects, not mongoose documents by passing `lean`
Adventure.findById(id, 'name', { lean: true }, function (err, doc) {});
// same as above
Adventure.findById(id, 'name').lean().exec(function (err, doc) {});
_id
to query by Query.prototype.setOptions()
Issue a MongoDB findOneAndDelete()
command by a document's _id field. In other words, findByIdAndDelete(id)
is a shorthand for findOneAndDelete({ _id: id })
.
This function triggers the following middleware.
findOneAndDelete()
_id
to query by Query.prototype.setOptions()
Issue a mongodb findAndModify remove command by a document's _id field. findByIdAndRemove(id, ...)
is equivalent to findOneAndRemove({ _id: id }, ...)
.
Finds a matching document, removes it, passing the found document (if any) to the callback.
Executes the query if callback
is passed.
This function triggers the following middleware.
findOneAndRemove()
sort
: if multiple docs are found by the conditions, sets the sort order to choose which doc to updateselect
: sets the document fields to returnrawResult
: if true, returns the raw result from the MongoDB driver
strict
: overwrites the schema's strict mode option for this updateA.findByIdAndRemove(id, options, callback) // executes
A.findByIdAndRemove(id, options) // return Query
A.findByIdAndRemove(id, callback) // executes
A.findByIdAndRemove(id) // returns Query
A.findByIdAndRemove() // returns Query
_id
to query by Query.prototype.setOptions()
Query.lean()
and the Mongoose lean tutorial. undefined
when casting an update. In other words, if this is set, Mongoose will delete baz
from the update in Model.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server. Issues a mongodb findAndModify update command by a document's _id field. findByIdAndUpdate(id, ...)
is equivalent to findOneAndUpdate({ _id: id }, ...)
.
Finds a matching document, updates it according to the update
arg, passing any options
, and returns the found document (if any) to the callback. The query executes if callback
is passed.
This function triggers the following middleware.
findOneAndUpdate()
new
: bool - true to return the modified document rather than the original. defaults to falseupsert
: bool - creates the object if it doesn't exist. defaults to false.runValidators
: if true, runs update validators on this command. Update validators validate the update operation against the model's schema.setDefaultsOnInsert
: if this and upsert
are true, mongoose will apply the defaults specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on MongoDB's $setOnInsert
operator.sort
: if multiple docs are found by the conditions, sets the sort order to choose which doc to updateselect
: sets the document fields to returnrawResult
: if true, returns the raw result from the MongoDB driver
strict
: overwrites the schema's strict mode option for this updateA.findByIdAndUpdate(id, update, options, callback) // executes
A.findByIdAndUpdate(id, update, options) // returns Query
A.findByIdAndUpdate(id, update, callback) // executes
A.findByIdAndUpdate(id, update) // returns Query
A.findByIdAndUpdate() // returns Query
All top level update keys which are not atomic
operation names are treated as set operations:
Model.findByIdAndUpdate(id, { name: 'jason bourne' }, options, callback)
// is sent as
Model.findByIdAndUpdate(id, { $set: { name: 'jason bourne' }}, options, callback)
This helps prevent accidentally overwriting your document with { name: 'jason bourne' }
.
Values are cast to their appropriate types when using the findAndModify helpers. However, the below are not executed by default.
setDefaultsOnInsert
option to override.findAndModify
helpers support limited validation. You can enable these by setting the runValidators
options, respectively.
If you need full-fledged validation, use the traditional approach of first retrieving the document.
Model.findById(id, function (err, doc) {
if (err) ..
doc.name = 'jason bourne';
doc.save(callback);
});
Query.prototype.select()
Query.prototype.setOptions()
Finds one document.
The conditions
are cast to their respective SchemaTypes before the command is sent.
Note: conditions
is optional, and if conditions
is null or undefined, mongoose will send an empty findOne
command to MongoDB, which will return an arbitrary document. If you're querying by _id
, use findById()
instead.
// find one iphone adventures - iphone adventures??
Adventure.findOne({ type: 'iphone' }, function (err, adventure) {});
// same as above
Adventure.findOne({ type: 'iphone' }).exec(function (err, adventure) {});
// select only the adventures name
Adventure.findOne({ type: 'iphone' }, 'name', function (err, adventure) {});
// same as above
Adventure.findOne({ type: 'iphone' }, 'name').exec(function (err, adventure) {});
// specify options, in this case lean
Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }, callback);
// same as above
Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }).exec(callback);
// chaining findOne queries (same as above)
Adventure.findOne({ type: 'iphone' }).select('name').lean().exec(callback);
Query.prototype.setOptions()
Issue a MongoDB findOneAndDelete()
command.
Finds a matching document, removes it, and passes the found document (if any) to the callback.
Executes the query if callback
is passed.
This function triggers the following middleware.
findOneAndDelete()
This function differs slightly from Model.findOneAndRemove()
in that findOneAndRemove()
becomes a MongoDB findAndModify()
command, as opposed to a findOneAndDelete()
command. For most mongoose use cases, this distinction is purely pedantic. You should use findOneAndDelete()
unless you have a good reason not to.
sort
: if multiple docs are found by the conditions, sets the sort order to choose which doc to updatemaxTimeMS
: puts a time limit on the query - requires mongodb >= 2.6.0select
: sets the document fields to returnprojection
: like select, it determines which fields to return, ex. { projection: { _id: 0 } }
rawResult
: if true, returns the raw result from the MongoDB driver
strict
: overwrites the schema's strict mode option for this updateA.findOneAndDelete(conditions, options, callback) // executes
A.findOneAndDelete(conditions, options) // return Query
A.findOneAndDelete(conditions, callback) // executes
A.findOneAndDelete(conditions) // returns Query
A.findOneAndDelete() // returns Query
Values are cast to their appropriate types when using the findAndModify helpers. However, the below are not executed by default.
setDefaultsOnInsert
option to override.findAndModify
helpers support limited validation. You can enable these by setting the runValidators
options, respectively.
If you need full-fledged validation, use the traditional approach of first retrieving the document.
Model.findById(id, function (err, doc) {
if (err) ..
doc.name = 'jason bourne';
doc.save(callback);
});
Query.prototype.setOptions()
Issue a mongodb findAndModify remove command.
Finds a matching document, removes it, passing the found document (if any) to the callback.
Executes the query if callback
is passed.
This function triggers the following middleware.
findOneAndRemove()
sort
: if multiple docs are found by the conditions, sets the sort order to choose which doc to updatemaxTimeMS
: puts a time limit on the query - requires mongodb >= 2.6.0select
: sets the document fields to returnprojection
: like select, it determines which fields to return, ex. { projection: { _id: 0 } }
rawResult
: if true, returns the raw result from the MongoDB driver
strict
: overwrites the schema's strict mode option for this updateA.findOneAndRemove(conditions, options, callback) // executes
A.findOneAndRemove(conditions, options) // return Query
A.findOneAndRemove(conditions, callback) // executes
A.findOneAndRemove(conditions) // returns Query
A.findOneAndRemove() // returns Query
Values are cast to their appropriate types when using the findAndModify helpers. However, the below are not executed by default.
setDefaultsOnInsert
option to override.findAndModify
helpers support limited validation. You can enable these by setting the runValidators
options, respectively.
If you need full-fledged validation, use the traditional approach of first retrieving the document.
Model.findById(id, function (err, doc) {
if (err) ..
doc.name = 'jason bourne';
doc.save(callback);
});
Query.prototype.setOptions()
Query.lean()
. undefined
when casting an update. In other words, if this is set, Mongoose will delete baz
from the update in Model.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server. Issue a MongoDB findOneAndReplace()
command.
Finds a matching document, replaces it with the provided doc, and passes the returned doc to the callback.
Executes the query if callback
is passed.
This function triggers the following query middleware.
findOneAndReplace()
sort
: if multiple docs are found by the conditions, sets the sort order to choose which doc to updatemaxTimeMS
: puts a time limit on the query - requires mongodb >= 2.6.0select
: sets the document fields to returnprojection
: like select, it determines which fields to return, ex. { projection: { _id: 0 } }
rawResult
: if true, returns the raw result from the MongoDB driver
strict
: overwrites the schema's strict mode option for this updateA.findOneAndReplace(conditions, options, callback) // executes
A.findOneAndReplace(conditions, options) // return Query
A.findOneAndReplace(conditions, callback) // executes
A.findOneAndReplace(conditions) // returns Query
A.findOneAndReplace() // returns Query
Values are cast to their appropriate types when using the findAndModify helpers. However, the below are not executed by default.
setDefaultsOnInsert
option to override.Query.prototype.setOptions()
Query.lean()
and the Mongoose lean tutorial. undefined
when casting an update. In other words, if this is set, Mongoose will delete baz
from the update in Model.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server. Issues a mongodb findAndModify update command.
Finds a matching document, updates it according to the update
arg, passing any options
, and returns the found document (if any) to the callback. The query executes if callback
is passed else a Query object is returned.
new
: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)upsert
: bool - creates the object if it doesn't exist. defaults to false.fields
: {Object|String} - Field selection. Equivalent to .select(fields).findOneAndUpdate()
maxTimeMS
: puts a time limit on the query - requires mongodb >= 2.6.0sort
: if multiple docs are found by the conditions, sets the sort order to choose which doc to updaterunValidators
: if true, runs update validators on this command. Update validators validate the update operation against the model's schema.setDefaultsOnInsert
: if this and upsert
are true, mongoose will apply the defaults specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on MongoDB's $setOnInsert
operator.rawResult
: if true, returns the raw result from the MongoDB driver
strict
: overwrites the schema's strict mode option for this updateA.findOneAndUpdate(conditions, update, options, callback) // executes
A.findOneAndUpdate(conditions, update, options) // returns Query
A.findOneAndUpdate(conditions, update, callback) // executes
A.findOneAndUpdate(conditions, update) // returns Query
A.findOneAndUpdate() // returns Query
All top level update keys which are not atomic
operation names are treated as set operations:
var query = { name: 'borne' };
Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)
// is sent as
Model.findOneAndUpdate(query, { $set: { name: 'jason bourne' }}, options, callback)
This helps prevent accidentally overwriting your document with { name: 'jason bourne' }
.
Values are cast to their appropriate types when using the findAndModify helpers. However, the below are not executed by default.
setDefaultsOnInsert
option to override.findAndModify
helpers support limited validation. You can enable these by setting the runValidators
options, respectively.
If you need full-fledged validation, use the traditional approach of first retrieving the document.
Model.findById(id, function (err, doc) {
if (err) ..
doc.name = 'jason bourne';
doc.save(callback);
});
Query.lean()
and the Mongoose lean tutorial. Implements $geoSearch
functionality for Mongoose
This function does not trigger any middleware
var options = { near: [10, 10], maxDistance: 5 };
Locations.geoSearch({ type : "house" }, options, function(err, res) {
console.log(res);
});
near
{Array} x,y point to search formaxDistance
{Number} the maximum distance from the point near that a result can belimit
{Number} The maximum number of results to returnlean
{Object|Boolean} return the raw object instead of the Mongoose ModelShortcut for creating a new Document from existing raw data, pre-saved in the DB. The document returned has no paths marked as modified initially.
// hydrate previous data into a Mongoose document
var mongooseCandy = Candy.hydrate({ _id: '54108337212ffb6d459f854c', type: 'jelly bean' });
This function is responsible for building indexes, unless autoIndex
is turned off.
Mongoose calls this function automatically when a model is created using mongoose.model()
or connection.model()
, so you don't need to call it. This function is also idempotent, so you may call it to get back a promise that will resolve when your indexes are finished building as an alternative to MyModel.on('index')
var eventSchema = new Schema({ thing: { type: 'string', unique: true }})
// This calls `Event.init()` implicitly, so you don't need to call
// `Event.init()` on your own.
var Event = mongoose.model('Event', eventSchema);
Event.init().then(function(Event) {
// You can also use `Event.on('index')` if you prefer event emitters
// over promises.
console.log('Indexes are done building!');
});
insertMany()
with ordered = false
is called an "unordered" insertMany()
. true
, will return the raw result from the MongoDB driver with a mongoose
property that contains validationErrors
if this is an unordered insertMany
. Shortcut for validating an array of documents and inserting them into MongoDB if they're all valid. This function is faster than .create()
because it only sends one operation to the server, rather than one for each document.
Mongoose always validates each document before sending insertMany
to MongoDB. So if one document has a validation error, no documents will be saved, unless you set the ordered
option to false.
This function does not trigger save middleware.
This function triggers the following middleware.
insertMany()
var arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
Movies.insertMany(arr, function(error, docs) {});
Helper for console.log. Given a model named 'MyModel', returns the string 'Model { MyModel }'
.
const MyModel = mongoose.model('Test', Schema({ name: String }));
MyModel.inspect(); // 'Model { Test }'
console.log(MyModel); // Prints 'Model { Test }'
undefined
if callback is specified, returns a promise if no callback. Lists the indexes currently defined in MongoDB. This may or may not be the same as the indexes defined in your schema depending on whether you use the autoIndex
option and if you build indexes manually.
Executes a mapReduce command.
o
is an object specifying all mapReduce options as well as the map and reduce functions. All options are delegated to the driver implementation. See node-mongodb-native mapReduce() documentation for more detail about options.
This function does not trigger any middleware.
var o = {};
// `map()` and `reduce()` are run on the MongoDB server, not Node.js,
// these functions are converted to strings
o.map = function () { emit(this.name, 1) };
o.reduce = function (k, vals) { return vals.length };
User.mapReduce(o, function (err, results) {
console.log(results)
})
query
{Object} query filter object.sort
{Object} sort input objects using this keylimit
{Number} max number of documentskeeptemp
{Boolean, default:false} keep temporary datafinalize
{Function} finalize functionscope
{Object} scope variables exposed to map/reduce/finalize during executionjsMode
{Boolean, default:false} it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.Xverbose
{Boolean, default:false} provide statistics on job execution time.readPreference
{String}out*
{Object, default: {inline:1}} sets the output target for the map reduce job.{inline:1}
the results are returned in an array{replace: 'collectionName'}
add the results to collectionName: the results replace the collection{reduce: 'collectionName'}
add the results to collectionName: if dups are detected, uses the reducer / finalize functions{merge: 'collectionName'}
add the results to collectionName: if dups exist the new docs overwrite the oldIf options.out
is set to replace
, merge
, or reduce
, a Model instance is returned that can be used for further querying. Queries run against this model are all executed with the lean
option; meaning only the js object is returned and no Mongoose magic is applied (getters, setters, etc).
var o = {};
// You can also define `map()` and `reduce()` as strings if your
// linter complains about `emit()` not being defined
o.map = 'function () { emit(this.name, 1) }';
o.reduce = 'function (k, vals) { return vals.length }';
o.out = { replace: 'createdCollectionNameForResults' }
o.verbose = true;
User.mapReduce(o, function (err, model, stats) {
console.log('map reduce took %d ms', stats.processtime)
model.find().where('value').gt(10).exec(function (err, docs) {
console.log(docs);
});
})
// `mapReduce()` returns a promise. However, ES6 promises can only
// resolve to exactly one value,
o.resolveToObject = true;
var promise = User.mapReduce(o);
promise.then(function (res) {
var model = res.model;
var stats = res.stats;
console.log('map reduce took %d ms', stats.processtime)
return model.find().where('value').gt(10).exec();
}).then(function (docs) {
console.log(docs);
}).then(null, handleError).end()
populate()
retain null
and undefined
array entries. localField
. By default, Mongoose gets the raw value of localField
. For example, you would need to set this option to true
if you wanted to add a lowercase
getter to your localField
. BlogPost.find().populate('author')
, blog posts with the same author will share 1 copy of an author
doc. Enable this option to make Mongoose clone populated docs before assigning them. localField
and foreignField
schemas don't line up. If you enable this option, Mongoose will instead filter out any localField
properties that cannot be casted to foreignField
's schema type. limit
and lean
. err
and the doc(s)
. Populates document references.
path
to an array. Inferred from schema by default.// populates a single object
User.findById(id, function (err, user) {
var opts = [
{ path: 'company', match: { x: 1 }, select: 'name' },
{ path: 'notes', options: { limit: 10 }, model: 'override' }
];
User.populate(user, opts, function (err, user) {
console.log(user);
});
});
// populates an array of objects
User.find(match, function (err, users) {
var opts = [{ path: 'company', match: { x: 1 }, select: 'name' }];
var promise = User.populate(users, opts);
promise.then(console.log).end();
})
// imagine a Weapon model exists with two saved documents:
// { _id: 389, name: 'whip' }
// { _id: 8921, name: 'boomerang' }
// and this schema:
// new Schema({
// name: String,
// weapon: { type: ObjectId, ref: 'Weapon' }
// });
var user = { name: 'Indiana Jones', weapon: 389 };
Weapon.populate(user, { path: 'weapon', model: 'Weapon' }, function (err, user) {
console.log(user.weapon.name); // whip
})
// populate many plain objects
var users = [{ name: 'Indiana Jones', weapon: 389 }]
users.push({ name: 'Batman', weapon: 8921 })
Weapon.populate(users, { path: 'weapon' }, function (err, users) {
users.forEach(function (user) {
console.log('%s uses a %s', users.name, user.weapon.name)
// Indiana Jones uses a whip
// Batman uses a boomerang
});
});
// Note that we didn't need to specify the Weapon model because
// it is in the schema's ref
Additional properties to attach to the query when calling save()
and isNew
is false.
Creates a Query
and specifies a $where
condition.
Sometimes you need to query for things in mongodb using a JavaScript expression. You can do so via find({ $where: javascript })
, or you can use the mongoose shortcut method $where via a Query chain or from your mongoose Model.
Blog.$where('this.username.indexOf("val") !== -1').exec(function (err, docs) {});
Base Mongoose instance the model uses.
If this is a discriminator model, baseModelName
is the name of the base model.
Collection the model uses.
This property is read-only. Modifying this property is a no-op.
Connection the model uses.
Alias for remove
Removes this document from the db. Equivalent to .remove()
.
product = await product.deleteOne();
await Product.findById(product._id); // null
Registered discriminators for this model.
Signal that we desire an increment of this documents version.
Model.findById(id, function (err, doc) {
doc.increment();
doc.save(function (err) { .. })
})
Returns another Model instance.
var doc = new Tank;
doc.model('User').findById(id, callback);
The name of the model
Removes this document from the db.
product.remove(function (err, product) {
if (err) return handleError(err);
Product.findById(product._id, function (err, product) {
console.log(product) // null
})
})
As an extra measure of flow control, remove will return a Promise (bound to fn
if passed) so it could be chained, or hooked to recieve errors
product.remove().then(function (product) {
...
}).catch(function (err) {
assert.ok(err)
})
w
option instead. writeConcern
option save()
has been journaled before resolving the returned promise. Overrides the schema-level writeConcern
option writeConcern
option. false
to skip that check. See restrictions on field names false
and timestamps are enabled, skip timestamps for this save()
. Saves this document.
product.sold = Date.now();
product = await product.save();
If save is successful, the returned promise will fulfill with the document saved.
const newProduct = await product.save();
newProduct === product; // true
Schema the model uses.
Removes all documents that match conditions
from the collection. To remove just the first document that matches conditions
, set the single
option to true.
const res = await Character.remove({ name: 'Eddard Stark' });
res.deletedCount; // Number of documents removed
This method sends a remove command directly to MongoDB, no Mongoose documents are involved. Because no Mongoose documents are involved, Mongoose does not execute document middleware.
Query.prototype.setOptions()
undefined
when casting an update. In other words, if this is set, Mongoose will delete baz
from the update in Model.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server. false
and schema-level timestamps are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set. function(error, res) {}
where res
has 3 properties: n
, nModified
, ok
. Same as update()
, except MongoDB replace the existing document with the given document (no atomic operators like $set
).
const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
res.n; // Number of documents matched
res.nModified; // Number of documents modified
This function triggers the following middleware.
replaceOne()
ClientSession
Requires MongoDB >= 3.6.0. Starts a MongoDB session for benefits like causal consistency, retryable writes, and transactions.
Calling MyModel.startSession()
is equivalent to calling MyModel.db.startSession()
.
This function does not trigger any middleware.
const session = await Person.startSession();
let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
await doc.remove();
// `doc` will always be null, even if reading from a replica set
// secondary. Without causal consistency, it is possible to
// get a doc back from the below query if the query reads from a
// secondary that is experiencing replication lag.
doc = await Person.findOne({ name: 'Ned Stark' }, null, { session, readPreference: 'secondary' });
ensureIndexes()
undefined
if callback is specified, returns a promise if no callback. Makes the indexes in MongoDB match the indexes defined in this model's schema. This function will drop any indexes that are not defined in the model's schema except the _id
index, and build any indexes that are in your schema but not in MongoDB.
See the introductory blog post for more information.
const schema = new Schema({ name: { type: String, unique: true } });
const Customer = mongoose.model('Customer', schema);
await Customer.createIndex({ age: 1 }); // Index is not in schema
// Will drop the 'age' index and create an index on `name`
await Customer.syncIndexes();
Translate any aliases fields/conditions so the final query or document object is pure
Character
.find(Character.translateAliases({
'名': 'Eddard Stark' // Alias for 'name'
})
.exec(function(err, characters) {})
Only translate arguments of object type anything else is returned raw
Query.prototype.setOptions()
undefined
when casting an update. In other words, if this is set, Mongoose will delete baz
from the update in Model.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server. filter
. upsert
are true, mongoose will apply the defaults specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on MongoDB's $setOnInsert
operator. false
and schema-level timestamps are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set. doc
, Mongoose will wrap doc
in $set
for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding $set
. Updates one document in the database without returning it.
This function triggers the following middleware.
update()
MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
const res = await MyModel.update({ name: 'Tobi' }, { ferret: true });
res.n; // Number of documents that matched `{ name: 'Tobi' }`
// Number of documents that were changed. If every doc matched already
// had `ferret` set to `true`, `nModified` will be 0.
res.nModified;
strict
(boolean): overrides the schema-level strict
option for this updateupsert
(boolean): whether to create the doc if it doesn't match (false)writeConcern
(object): sets the write concern for replica sets. Overrides the schema-level write concern
omitUndefined
(boolean): If true, delete any properties whose value is undefined
when casting an update. In other words, if this is set, Mongoose will delete baz
from the update in Model.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server.multi
(boolean): whether multiple documents should be updated (false)runValidators
: if true, runs update validators on this command. Update validators validate the update operation against the model's schema.setDefaultsOnInsert
(boolean): if this and upsert
are true, mongoose will apply the defaults specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on MongoDB's $setOnInsert
operator.timestamps
(boolean): If set to false
and schema-level timestamps are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.overwrite
(boolean): disables update-only mode, allowing you to overwrite the doc (false)All update
values are cast to their appropriate SchemaTypes before being sent.
The callback
function receives (err, rawResponse)
.
err
is the error if any occurredrawResponse
is the full response from MongoAll top level keys which are not atomic
operation names are treated as set operations:
var query = { name: 'borne' };
Model.update(query, { name: 'jason bourne' }, options, callback);
// is sent as
Model.update(query, { $set: { name: 'jason bourne' }}, options, function(err, res));
// if overwrite option is false. If overwrite is true, sent without the $set wrapper.
This helps prevent accidentally overwriting all documents in your collection with { name: 'jason bourne' }
.
Be careful to not use an existing model instance for the update clause (this won't work and can cause weird behavior like infinite loops). Also, ensure that the update clause does not have an _id property, which causes Mongo to return a "Mod on _id not allowed" error.
Mongoose casts values and runs setters when using update. The following features are not applied by default.
If you need document middleware and fully-featured validation, load the document first and then use save()
.
Model.findOne({ name: 'borne' }, function (err, doc) {
if (err) ..
doc.name = 'jason bourne';
doc.save(callback);
})
Query.prototype.setOptions()
undefined
when casting an update. In other words, if this is set, Mongoose will delete baz
from the update in Model.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server. false
and schema-level timestamps are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set. function(error, res) {}
where res
has 3 properties: n
, nModified
, ok
. Same as update()
, except MongoDB will update all documents that match filter
(as opposed to just the first one) regardless of the value of the multi
option.
Note updateMany will not fire update middleware. Use pre('updateMany')
and post('updateMany')
instead.
const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
res.n; // Number of documents matched
res.nModified; // Number of documents modified
This function triggers the following middleware.
updateMany()
Query.prototype.setOptions()
undefined
when casting an update. In other words, if this is set, Mongoose will delete baz
from the update in Model.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server. false
and schema-level timestamps are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set. Same as update()
, except it does not support the multi
or overwrite
options.
filter
regardless of the value of the multi
option.replaceOne()
if you want to overwrite an entire document rather than using atomic operators like $set
.const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
res.n; // Number of documents matched
res.nModified; // Number of documents modified
This function triggers the following middleware.
updateOne()
Casts and validates the given object against this model's schema, passing the given context
to custom validators.
const Model = mongoose.model('Test', Schema({
name: { type: String, required: true },
age: { type: Number, required: true }
});
try {
await Model.validate({ name: null }, ['name'])
} catch (err) {
err instanceof mongoose.Error.ValidationError; // true
Object.keys(err.errors); // ['name']
}
Requires a replica set running MongoDB >= 3.6.0. Watches the underlying collection for changes using MongoDB change streams.
This function does not trigger any middleware. In particular, it does not trigger aggregate middleware.
const doc = await Person.create({ name: 'Ned Stark' });
const changeStream = Person.watch().on('change', change => console.log(change));
// Will print from the above `console.log()`:
// { _id: { _data: ... },
// operationType: 'delete',
// ns: { db: 'mydb', coll: 'Person' },
// documentKey: { _id: 5a51b125c5500f5aa094c7bd } }
await doc.remove();
Creates a Query, applies the passed conditions, and returns the Query.
For example, instead of writing:
User.find({age: {$gte: 21, $lte: 65}}, callback);
we can instead write:
User.where('age').gte(21).lte(65).exec(callback);
Since the Query class also supports where
you can continue chaining
User
.where('age').gte(21).lte(65)
.where('name', /^b/i)
... etc
© 2010 LearnBoost
Licensed under the MIT License.
https://mongoosejs.com/docs/api/model.html