Wednesday, May 29, 2024
 Popular · Latest · Hot · Upcoming
-4
rated 0 times [  3] [ 7]  / answers: 1 / hits: 32898  / 10 Years ago, thu, november 13, 2014, 12:00:00

I use the following mongoose query in a MEAN-environment to find and output a particular author and his corresponding books.



Author
.findOne({personcode: code})
.select('-_id')
.select('-__v')
.populate('bookids') //referencing to book documents in another collection (->array of bookids)
.select('-_id') //this doens't affect the data coming from the bookids-documents
.select('-__v') //this doens't affect the data coming from the bookids-documents
.exec(function (err, data) {
//foo
});


I would also like to exclude the _id and __v fields from the populated data coming from the external documents. How can that be achieved?


More From » node.js

 Answers
5

The second parameter of populate is a field selection string, so you can do this as:



Author
.findOne({personcode: code})
.select('-_id -__v')
.populate('bookids', '-_id -__v')
.exec(function (err, data) {
//foo
});


Note that you should combine your field selections into a single string.


[#68814] Tuesday, November 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlynd

Total Points: 470
Total Questions: 108
Total Answers: 120

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;