0% found this document useful (0 votes)
39 views2 pages

Mongoose Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views2 pages

Mongoose Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

MONGOOSE FUNCTIONS:

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It
provides a straightforward, schema-based solution to model application data and
includes built-in functions for working with MongoDB.

Here’s a categorized list of commonly used Mongoose functions:

Schema-Related Functions

Schema(): Defines the structure of documents in a collection.


add(): Adds additional paths to an existing schema.
path(): Gets or sets schema paths.
index(): Defines an index for a schema field.
pre(): Middleware executed before specific actions (e.g., save, validate).
post(): Middleware executed after specific actions.
virtual(): Defines virtual fields (fields not persisted in the database).
method(): Adds custom instance methods to schema documents.
statics(): Adds custom static methods to the model.
plugin(): Adds plugins to extend schema functionality.
set(): Sets schema options.
get(): Retrieves schema options or properties.
remove(): Removes paths or middleware from a schema.
clone(): Creates a copy of the schema.
Model-Related Functions
model(): Creates a Mongoose model based on a schema.
discriminator(): Inherits and extends a base model.
populate(): Fills referenced documents in a query.

Model-Related Functions

model(): Creates a Mongoose model based on a schema.


discriminator(): Extends a base model to create hierarchical schemas.
populate(): Populates referenced documents in queries.

Document Instance Methods

save(): Saves a document to the database.


remove(): Removes a document from the database.
validate(): Validates the document against the schema.
toObject(): Converts a document to a plain JavaScript object.
toJSON(): Converts a document to a JSON object.
populate(): Populates referenced fields in a document.
increment(): Increments a document's version key.
isModified(): Checks if a path or the entire document has been modified.
markModified(): Marks a path as modified.
equals(): Checks if this document equals another document.

Model (Query) Functions

create(): Creates and saves one or more documents to the database.


find(): Retrieves all matching documents.
findById(): Finds a document by its ID.
findOne(): Retrieves the first matching document.
findOneAndUpdate(): Updates a document and returns it.
findOneAndDelete(): Deletes a document and returns it.
findByIdAndUpdate(): Updates a document by its ID and returns it.
findByIdAndDelete(): Deletes a document by its ID and returns it.
countDocuments(): Counts the number of documents matching a query.
distinct(): Finds distinct values for a specific field.
updateOne(): Updates a single document.
updateMany(): Updates multiple documents.
deleteOne(): Deletes a single document.
deleteMany(): Deletes multiple documents.
aggregate(): Performs aggregation operations on data.
replaceOne(): Replaces a document entirely.
exists(): Checks if any documents match a query

Connection-Related Functions

connect(): Establishes a connection to MongoDB.


disconnect(): Closes the connection to MongoDB.
connection: Provides access to the underlying MongoDB connection instance.
startSession(): Starts a session for MongoDB transactions

Utility Functions

isValidObjectId(): Checks if a value is a valid ObjectId.


cast(): Casts a value to a specific schema type.
lean(): Retrieves plain JavaScript objects instead of Mongoose documents.
modelNames(): Returns a list of all registered model names.
set(): Sets a global Mongoose option.
get(): Gets a global Mongoose option.

Middleware Functions
pre(): Adds middleware to execute before specific actions (e.g., save, find).
post(): Adds middleware to execute after specific actions.

You might also like