2014-11-13 15 views
8

Io uso la seguente query mangusta in un ambiente MEAN per trovare ed emettere un particolare autore e i suoi libri corrispondenti.Mongoose/Mongodb: esclude i campi dai dati di query popolate

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 
}); 

Vorrei anche di escludere il "_id" e "__v" campi dai dati popolate provenienti dai documenti esterni. Come può essere realizzato?

risposta

22

Il secondo parametro di populate è una stringa di selezione campo, in modo da poter fare questo come:

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

noti che si dovrebbe combinare le selezioni di campo in una singola stringa.

+0

grande! molte grazie. funziona bene =) –

0

Grazie JohnnyHK, e per il parametro oggetto funziona:

Entity.populate({ 
    path: 'bookids', 

    // some other properties 
    match: { 
     active: true 
    }, 
    // some other properties 

    select: '-_id -__v' // <-- this is the way 
}).then(...) // etc 
Problemi correlati