Mongoose: a multiple request is populated with a single call

In Mongoose, I can use the query to fill in additional fields after the query. I can also fill in several paths, for example

Person.find({}) .populate('books movie', 'title pages director') .exec() 

However, this will create a search in the book collecting fields for the title, pages and director, as well as a search in the film collecting fields for the title, pages and director. I want to get the title and pages only from books, and the director from the film. I could do something like this:

 Person.find({}) .populate('books', 'title pages') .populate('movie', 'director') .exec() 

which gives me the expected result and queries.

But is there a way to have the behavior of the second fragment using the same "one line" syntax, such as the first fragment? The reason for this is because I want to programmatically determine the arguments for the fill function and pass it. I cannot do this for multiple fill calls.

+42
javascript mongoose
Jan 12 '14 at
source share
2 answers

Having studied the source code of mongoose, I solved it with

 var populateQuery = [{path:'books', select:'title pages'}, {path:'movie', select:'director'}]; Person.find({}) .populate(populateQuery) .execPopulate() 
+100
Jan 13 '14 at 19:53
source share

you can also do something like:

 {path:'user',select:['key1','key2']} 
+1
Aug 29 '17 at 13:48 on
source share



All Articles