P粉0813607752023-09-03 00:53:03
I recommend the first technique - it's definitely cleaner and probably faster. You should make sure to index the author
field - just like you would index a foreign key in an RDB.
P粉6765887382023-09-03 00:41:40
There do exist some differences between the two methods
I find this approach more intuitive and readable, and therefore easier to maintain, especially for complex queries
However, it may not be the most efficient when dealing with large amounts of data, as it will make additional calls to the database for each nested field. This can become a performance bottleneck when dealing with large numbers of complex nested queries.
On the other hand, $lookup operations are generally more performant because they use a single database call to connect and retrieve data.
However, as query complexity increases, $lookup operations can become complex and difficult to read and maintain. It may require more knowledge of the MongoDB query language and may not be as simple and easy to use as the parser approach. Poorly written aggregates can cause performance issues, so be absolutely sure of what you're doing.
So in your example I don't mind using the parser approach, even though using aggregation is faster, the first approach will be easier to read/improve/maintain.
Hope it helps:)