gpt4 book ai didi

node.js - 无法通过 Mongoose 包含和排除查询字段

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:52 25 4
gpt4 key购买 nike

我有一些架构并进行如下查询:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com