简体   繁体   中英

MongoDB aggregation vs Mongoose virtuals

From this post I have understood that mongoose has created a framework around MongoDB that does some of it queries in a different way.

I have read about populate virtuals . When you can use them to get your desired results, what advantages and disadvantages do they have over MongoDB aggregations? When should you use one over the other?

Mongoose: is a very good framework around MongoDB that make it easy to work with MongoDB because most of the things are managed internally by Mongoose. Populate Virtuals is part of the framework and then will allow you to have basic entity-relationship resolved at driver-level.

Aggregation Framework: is a more advanced feature, you can think it as a query language (actually, it is) where you can create basic, medium, advanced query that are resolved at DB-level (this is huge difference in terms of performance). Rather than just "fetch" some data with $lookup, you can also filter the data or grouping by some key. Something you can't do with virtuals.

As general point, you can think Aggregation Framework as a very clean and extendible way to write performance oriented query in MongoDB since the whole pipeline is resolved at DB-level rather then driver-level.

NOTE: Aggregation Expressions can be also used as part of findAndModify update pipeline, that means you can compute a find and update the data by using a pipeline (something you can't do with virtuals).

NOTE: Upcoming version of MongoDB 4.4 will also introduce Aggregate Expressions in find(), that make the aggregate syntax a good investment for write you query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM