Mongoose / Mongodb:从填充的查询数据中排除字段
我在MEAN环境中使用以下mongoose查询来查找和输出特定的作者及其相应的书籍。
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 });
我也想排除来自外部文档的填充数据中的“_id”和“__v”字段。 这怎么能实现?
populate
的第二个参数是一个字段selectstring,所以你可以这样做:
Author .findOne({personcode: code}) .select('-_id -__v') .populate('bookids', '-_id -__v') .exec(function (err, data) { //foo });
请注意,您应该将您的字段select合并为一个string。
感谢JohnnyHK,并为对象参数工作:
Entity.populate({ path: 'bookids', // some other properties match: { active: true }, // some other properties select: '-_id -__v' // <-- this is the way }).then(...) // etc