How to do raw mongodb operations in mongoose?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Mongoose is a popular ODM (Object Document Mapper) library for MongoDB in Node.js, offering a structured way to interact with MongoDB databases and facilitating schema design, data validation, and business logic hooks. However, sometimes you may need the flexibility and power of raw MongoDB operations. Fortunately, Mongoose provides a way to perform these operations directly on collections while maintaining the benefits of its connection and model management.
Understanding Mongoose and Raw MongoDB Operations
While Mongoose excels at abstracting MongoDB's native syntax, it can be advantageous to perform raw operations for:
- Performance: When executing complex operations that would benefit from the raw power of MongoDB.
- Flexibility: When a particular MongoDB feature isn't supported directly by Mongoose or when requiring fine-grained control over the query.
- Compatibility: When using plugins or integrations that necessitate raw commands.
Creating a Mongoose Model
Before diving into raw operations, you should have a working Mongoose model to interact with.
Accessing the Native MongoDB Collection
Mongoose models have an internal method called collection, which provides direct access to the MongoDB native collection methods.
Performing Raw Operations
1. Find Documents
To find documents, you can use the find method provided by the collection object.
2. Insert Documents
Inserting documents can be performed using the insertOne or insertMany methods.
3. Update Documents
For updates, you can leverage updateOne or updateMany.
4. Delete Documents
Documents can be removed using deleteOne or deleteMany.
5. Aggregations
MongoDB's powerful aggregation pipeline can be accessed similarly.
A Summary of Operations in Mongoose
Here's a quick summary table of typical raw operations you might perform using Mongoose's collection:
| Operation | Method | Description |
| Find | find | Retrieves documents based on a filter. |
| Insert | insertOne, insertMany | Adds new documents to the collection. |
| Update | updateOne, updateMany | Modifies existing documents based on a filter. |
| Delete | deleteOne, deleteMany | Removes documents matching a filter. |
| Aggregate | aggregate | Performs complex data processing and transformations. |
Conclusion
While Mongoose provides a rich API for interacting with MongoDB using its schema-based approach, there are scenarios where raw MongoDB operations are necessary for their flexibility and capability. Mongoose models offer direct access to their underlying collections, allowing you to harness the full power of MongoDB without leaving the comfort of Mongoose's organization and connection management. Whether you're optimizing your queries or integrating with systems dependent on raw commands, understanding these operations is an essential part of any Node.js and MongoDB developer's toolkit.
Related reading
- How to do SQL Like in Linq?
- How to do this in Laravel, subquery where in
- How to drop a PostgreSQL database if there are active connections to it?
- How to drop a table if it exists?
- How to dynamically build a JSON object?
- How to embed HTML into IPython output?
- How to drop or delete a collection in MongoDB?
- How to drop unique in MySQL?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.