Mongoose Model API

Mongoose's model API provides tools for working with predefined Mongoose Model's. It's the primary API that you will use to manipulate documents in MongoDB:

Property Example Description
remove Posts.remove({title: 'foobar'}); Removes matching documents from the db.
aggregate Posts.aggregate() .group({}) .select({}); Performs data aggregations on the models collection.
bulkWrite Posts.bulkWrite([{ insertOne: {} }, { updateOne: {} }, { deleteOne: {} }]); Combines multiple create, update, and delete operations into a single MongoDB request.
count Posts.count({}); Retrieves the count of items that match.
create Posts.create(new Post({})); Creates and saves one or more documents passed to it.
deleteMany Posts.deleteMany({}); Very similar to remove, but will always remove any matches regardless of options provided.
deleteOne Posts.deleteOne({}); Very similar to remove, but will always only remove a single document.
discriminator Posts.discriminator('Review', ReviewSchema); Defines a discriminator for a model, a way to map different schema models to the same collection.
distinct Posts.distinct('title'); Defines a distinct query that will return a list of unique values for the field on the same collection.
ensureIndexes Posts.ensureIndexes(); Requests that MongoDB confirm it has created indexes for all the indexed fields in the collection.
find Posts.find({title: 'foobar'}); Returns any documents that match, but default returns all records in the collection.
findById Posts.findById(123); Returns a specific document that matches the ID provided.
findByIdAndRemove Posts.findByIdAndRemove(123); Finds and removes a specific document that matches the ID provided.
findByIdAndUpdate Posts.findByIdAndUpdate(123, {}); Finds and updates a specific document that matches the ID provided, with the provided update object.
findOne Posts.findOne({}); Returns the first matching document, with the provided update object.
findOneAndRemove Posts.findOneAndRemove({}); Removes the first matching document, with the provided update object.
findOneAndUpdate Posts.findOneAndUpdate({}, {}); Updates the first matching document, with the provided update object.
geoNear Places.geoNear([4,5], { maxDistance : 5, spherical : true }); Performs a geospatial-based nearby query for documents in a collection.
geoSearch Places.geoSearch({ type : "restaurant" }); Performs a geospatial-based search for documents in a collection.
hydrate var post = Posts.hydrate({ _id: '54108337212ffb6d459f854c', title: 'foobar' }); Creating a new document from existing raw data. Mostly used for converting fixture data into Mongoose documents.
insertMany Posts.insertMany([]); Very similar to create, but will add all the documents as one MongoDB request.
mapReduce Posts.mapReduce({ map: {}, reduce: {}}); Executes a map reduce command.
populate Posts.populate(post, {path: 'author'}); Populates a subdocument reference inside the provided document. We will discuss this feature in detail in Chapter 8.
replaceOne Posts.replaceOne({}); Very similar to update, except it will completely replace the document.
translateAliases Posts.find(Posts.translateAliases({'alias': 'foobar' }); Converts alias values for purposes of making a query to MongoDB.
update Posts.update({}, {}); Updates the matching documents with the provided object.
updateMany Posts.updateMany({}, {}); Updates all the matching documents with the provided object, regardless of options provided.
updateOne Posts.updateOne({}, {}); Updates the first matching document with the provided object, regardless of options provided.
where Post.where('title', 'foobar'); Allows you to provide a query for a field and value.
$where Post.$where('this.title !== "foobar"'); Allows you to provide a JavaScript expression as a query to MongoDB.
base Post.base; If using discriminators, this is the base model instance for this model.
baseModelName Post.baseModelName; If using discriminators, this is the base model name for this model.
collection Post.collection; The collection for this model.
db Post.db; The current MongoDB database connection.
discriminators Post.descriminators; A list of discriminators for this model.
modelName Post.modelName; The name of this model.
schema Post.schema; The schema for this model.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.141.23