MongoDB:无法规范化查询:BadValue Projection 不能混合包含和排除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24949544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB: Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion
提问by Sadikhasan
I am newer in MongoDB with CakePHP.
我在使用 CakePHP 的 MongoDB 中比较新。
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")
}
When I run the following query to get only name
, it returns an error:
当我运行以下查询以获取 only 时name
,它返回一个错误:
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
}
When I run the following query to get only name
with _id:0
, then it executes well:
当我运行以下查询只获取name
with 时_id:0
,它执行得很好:
db.testData.find({},{name:1, _id:0})
{ "name" : "sadikhasan" }
My question is, why I am getting an error when I write created:0
in the projection list. Thanks for help in advance.
我的问题是,为什么我created:0
在投影列表中写入时会出错。提前感谢您的帮助。
回答by Neil Lunn
You cannot mix inclusion and exclusion, the onlyexception is the _id
field.
不能混合包含和排除,唯一的例外是_id
字段。
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 })
Or this:
或这个:
db.collection.find({},{ "_id": 0, "created": 0 })
But it is not allowed to "mix" any other operations other than "_id"
但不允许“混合”除“_id”之外的任何其他操作
db.collection.find({},{ "_id": 0, "name": 1, "created": 0 })
That would also produce an error.
那也会产生错误。
This is all covered in the manual pages.
回答by Bipul Sinha
It is throwing error "Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion." becuase you are mixing both inclusion and exlusion. 1 stands for inclusion and 0 stands for exclusion. You can use either 0 or 1 in your query. So, in case you wish to see , say, only _id and name fields, use can either use: 1) Inclusion:
它抛出错误“无法规范化查询:BadValue Projection 不能混合包含和排除。” 因为你混合了包含和排除。1 代表包含,0 代表排除。您可以在查询中使用 0 或 1。因此,如果您希望仅查看 _id 和 name 字段,则使用可以使用:1)包含:
db.testdata.find({}, {_id:1,name:1})
Or 2) Exclusion:
或 2) 排除:
db.testdata.find({},{created:0})
In both the above scenarios, it will show only _id and name field.
在上述两种情况下,它只会显示 _id 和 name 字段。
回答by Akash prasad
I ran into the same problem. It means that you cannot tell MongoDB to select a particular field and unselect another field at the same time.
我遇到了同样的问题。这意味着您不能告诉 MongoDB 选择特定字段并同时取消选择另一个字段。
this is wrong, cannot specify like this in the select options Error: -Projection cannot have a mix of inclusion and exclusion.
这是错误的,不能在选择选项中这样指定错误:-投影不能混合包含和排除。
reviewSchema.pre(/^find/, function (next) {
this.populate({
path: 'tour',
select: 'name -__v'
})
next();
});
Correct format.
正确的格式。
reviewSchema.pre(/^find/, function (next) {
this.populate({
path: 'tour',
select: 'name' // Removed -__v
})
next();
});
回答by SuRa
In simple words to describe the solution, you can tell the MongoDB either what are all the fields you want or you don't want from MongoDB.
用简单的话来描述解决方案,您可以告诉 MongoDB 您想要或不想要 MongoDB 的所有字段。
which means in the select method you have to mention only required or not required fields. like below
这意味着在选择方法中,您只需要提及必填或非必填字段。像下面
find({<!--query here--!>}).select({
password: 0,
allowed_ip: 0,
username: 0,
password: 0,
__v: 0,
_id: 0
});
It should be either zero(exclusions/ not required) or one(inclusions/required fields.) cheers!!!
它应该是零(排除/不需要)或一(包含/必填字段。)欢呼!!!