MongoDB:无法规范化查询:BadValue投影不能混合包含和排除 [英] MongoDB: Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion
问题描述
我在使用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
}
当我运行以下查询以仅获取name
和_id:0
时,它执行得很好:
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.
例如,如果您有以下内容:
For example if you have this:
{
"_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投影不能混合包含和排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!