Mongoose/Mongodb:从填充的查询数据中排除字段 [英] Mongoose/Mongodb: Exclude fields from populated query data

查看:87
本文介绍了Mongoose/Mongodb:从填充的查询数据中排除字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MEAN环境中使用以下猫鼬查询来查找和输出特定作者及其对应的书.

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

我还想从来自外部文档的填充数据中排除"_id"和"__v"字段.如何实现?

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?

推荐答案

populate 是字段选择字符串,因此您可以这样操作:

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.

这篇关于Mongoose/Mongodb:从填充的查询数据中排除字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆