Who has better performance than mongoose’s $lookup?

  mongodb, question

Three Questions to Ask

1. What is the implementation logic of 1.mongoose population? Is to encapsulate a lot of find, and then useDBRefsIs there a query based on the pattern?
2. Did 2.mongodb aggregate query IO only once? It’s$lookupCompared with the traditional methodDBRefsIs it better or faster to do multi-table association queries? Why?
3. Is there any way to monitor the program’s reading and writing of mongodb and test the speed?

Remarks:Personally, I thinkmongoDBAggregate of$lookupIt’s too much trouble to write.

MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections.

AndDBRefOnly according toObjectId, and has a fixed format requirement, so I tend to think it is constructed query, when get the data object query again according to the reference conditions to get the referenced object. This leads to an inevitable problem. If a query returns 10 records in the result set, then in addition to these records, 10 additional queries will be made to obtain the objects referencing them. There must be a big discount on performance.

  • $lookupPerformance ratio ofDBRefWell, it only makes one query to get all the results. However, this operator can only be used in replication sets at present, and fragmentation sets are not supported for the time being.
  • How much time the program spends on MongoDB queries, of course, must be monitored from the program side. MongoDB will only give how long the query takes, and most of the time the bulk of the consumption may also occur on the network. If you are interested in how long it takes to execute the program in the database, please refer todatabase profiler. However, profiling in a production environment should be avoided because it may have a considerable impact on performance.

To sum up, whether it is$lookupOr ..DBRef, are suggested to avoid. I am hereThis problemLi explained the correct handling method and reason, please refer to.