MongoDB:无法规范化查询:BadValue Projection 不能混合包含和排除

编程入门 行业动态 更新时间:2024-10-23 15:28:47
本文介绍了MongoDB:无法规范化查询:BadValue Projection 不能混合包含和排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用 CakePHP 的 MongoDB 方面比较新.

I am newer in MongoDB with CakePHP.

当我编写以下查询时,它会执行得很好.

When I write the following query it will execute very well.

db.testData.find() { "_id" : ObjectId("53d1f79db8173a625961ff3d"), "name" : "sadikhasan", "created" : ISODate("2014-07-25T06:22:21.701Z") }

当我运行以下查询以仅获取 name 时,它返回一个错误:

When I run the following query to get only name, it returns an error:

db.testData.find({},{name:1, created:0}) error: { "$err" : "Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion.", "code" : 17287 }

当我运行以下查询以仅获取带有 _id:0 的 name 时,它执行得很好:

When I run the following query to get only name with _id:0, then it executes well:

db.testData.find({},{name:1, _id:0}) { "name" : "sadikhasan" }

我的问题是,为什么在投影列表中写入 created:0 时会出现错误.提前感谢您的帮助.

My question is, why I am getting an error when I write created:0 in the projection list. Thanks for help in advance.

推荐答案

不能混合包含和排除,唯一的例外是 _id 字段.

You cannot mix inclusion and exclusion, the only exception is the _id field.

例如,如果你有这个:

{ "_id": ObjectId("53d1fd30bdcf7d52c0d217de"), "name": "bill", "birthdate": ISODate("2014-07-80T00:00:00.000Z"), "created": ISODate("2014-07-25T06:44:38.641Z") }

如果您只需要姓名"和生日",您需要这样做:

If all you want is the "name" and "birthdate" you need to do this:

db.collection.find({},{ "_id": 0, "name": 1, "birthdate": 1 })

或者这个:

db.collection.find({},{ "_id": 0, "created": 0 })

但不允许混合"除_id"之外的任何其他操作

But it is not allowed to "mix" any other operations other than "_id"

db.collection.find({},{ "_id": 0, "name": 1, "created": 0 })

这也会产生错误.

手册页.

更多推荐

MongoDB:无法规范化查询:BadValue Projection 不能混合包含和排除

本文发布于:2023-10-18 11:13:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MongoDB   BadValue   Projection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!