values with which to create the document
Base Mongoose instance the model uses.
If this is a discriminator model, baseModelName is the name of the base model.
Collection the model uses.
Connection the model uses.
Registered discriminators for this model.
The name of the model
Schema the model uses.
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.
pipeline operator(s) or operator array
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.
Operations
callback
BulkWriteOpResult
if the operation succeeds
Counts number of matching documents in a database collection.
Counts number of documents matching criteria
in a database collection.
If you want to count all documents in a large collection,
use the estimatedDocumentCount()
instead.
If you call countDocuments({})
, MongoDB will always execute
a full collection scan and not use any indexes.
Shortcut for saving one or more documents to the database. MyModel.create(docs) does new MyModel(doc).save() for every doc in docs. Triggers the save() hook.
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.
Similar to ensureIndexes(), except for it uses the createIndex function. The ensureIndex() function checks to see if an index with that name already exists, and, if not, does not attempt to create the index. createIndex() bypasses this check.
Optional callback
Adds a discriminator type.
discriminator model name
discriminator model schema
the string stored in the discriminatorKey
property
Adds a discriminator type.
discriminator model name
discriminator model schema
the string stored in the discriminatorKey
property
Creates a Query for a distinct operation. Passing a callback immediately executes the query.
Sends ensureIndex commands to mongo for each index declared in the schema.
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.
Returns true if at least one document exists in the database that matches
the given filter
, and false otherwise.
Finds documents.
Finds a single document by its _id field. findById(id) is almost* equivalent to findOne({ _id: id }). findById() triggers findOne hooks.
value of _id to query by
Issue a mongodb findOneAndDelete command by a document's _id field. findByIdAndDelete(id, ...) is equivalent to findByIdAndDelete({ _id: id }, ...). Finds a matching document, removes it, passing the found document (if any) to the callback. Executes immediately if callback is passed, else a Query object is returned.
Note: same signatures as findByIdAndRemove
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 immediately if callback is passed, else a Query object is returned.
If mongoose option 'useFindAndModify': set to false it uses native findOneAndUpdate() rather than deprecated findAndModify(). https://mongoosejs.com/docs/api.html#mongoose_Mongoose-set
Note: same signatures as findByIdAndDelete
Issues a mongodb findAndModify update command by a document's _id field. findByIdAndUpdate(id, ...) is equivalent to findOneAndUpdate({ _id: id }, ...).
If mongoose option 'useFindAndModify': set to false it uses native findOneAndUpdate() rather than deprecated findAndModify(). https://mongoosejs.com/docs/api.html#mongoose_Mongoose-set
Finds one document. The conditions are cast to their respective SchemaTypes before the command is sent.
Issues a mongodb findOneAndDelete command. Finds a matching document, removes it, passing the found document (if any) to the callback. Executes immediately if callback is passed.
Note: same signatures as findOneAndRemove
Issue a mongodb findAndModify remove command. Finds a matching document, removes it, passing the found document (if any) to the callback. Executes immediately if callback is passed else a Query object is returned.
If mongoose option 'useFindAndModify': set to false it uses native findOneAndUpdate() rather than deprecated findAndModify(). https://mongoosejs.com/docs/api.html#mongoose_Mongoose-set
Note: same signatures as findOneAndDelete
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 immediately if callback is passed else a Query object is returned.
Implements $geoSearch functionality for Mongoose
an object that specifies the match condition (required)
for the geoSearch, some (near, maxDistance) are required
return the raw object instead of the Mongoose Model
The maximum number of results to return
the maximum distance from the point near that a result can be
x,y point to search for
optional callback
Shortcut for creating a new Document from existing raw data, pre-saved in the DB. The document returned has no paths marked as modified initially.
Performs any async initialization of this model against MongoDB.
This function is called automatically, 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')
optional
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. This function does not trigger save middleware.
Documents to insert.
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.
Returns undefined
if callback is specified, returns a promise if no callback.
Executes a mapReduce command.
an object specifying map-reduce options
Populates document references.
Either a single document or array of documents to populate.
A hash of key/val (path, options) used for population.
Optional callback, executed upon completion. Receives err and the doc(s).
Removes documents from the collection.
Same as update(), except MongoDB replace the existing document with the given document (no atomic operators like $set). This function triggers the following middleware: replaceOne
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.
options to pass to ensureIndexes()
optional callback
Returns undefined
if callback is specified, returns a promise if no callback.
Translate any aliases fields/conditions so the final query or document object is pure
fields/conditions that may contain aliased keys
the translated 'pure' fields/conditions
Updates documents in the database without returning them. All update values are cast to their appropriate SchemaTypes before being sent.
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.
Creates a Query, applies the passed conditions, and returns the Query.
Generated using TypeDoc
Model constructor Provides the interface to MongoDB collections as well as creates document instances.