我有一些架构并进行如下查询:
mongoose.model('mymodel').find({}, 'one -two -__v', function(err,docs){});
我希望文档只返回字段“one”。始终忽略字段“二”并其余。 然后我收到以下错误:
You cannot currently mix including and excluding fields. Contact us if this is an issue.
我正在动态生成字段字符串以包含或排除所需的字段。
如何消除错误。
最佳答案
这实际上与 Mongoose 无关。 “限制”存在于 mongo 本身。
来自collection.find documentation
<小时/>A projection cannot contain both include and exclude specifications, except for the exclusion of the
_id
field. In projections that explicitly include fields, the_id
field is the only field that you can explicitly exclude.
您可以包含、或排除,但不能同时执行两者(_id
除外)
仅选择_id
和一个
'one'
选择除两个
之外的所有字段
'-two'
异常(exception):仅选择一个
不选择_id
'one -_id'
关于node.js - 无法通过 Mongoose 包含和排除查询字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22806027/