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")
}
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
}
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" }
My question is, why I am getting an error when I write created:0
in the projection list. Thanks for help in advance.
You cannot mix inclusion and exclusion, the only exception is the
_id
field.For example if you have this:
If all you want is the "name" and "birthdate" you need to do this:
Or this:
But it is not allowed to "mix" any other operations other than "_id"
That would also produce an error.
This is all covered in the manual pages.
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.
this is wrong, cannot specify like this in the select options Error: -Projection cannot have a mix of inclusion and exclusion.
Correct format.
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:
Or 2) Exclusion:
In both the above scenarios, it will show only _id and name field.
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.
which means in the select method you have to mention only required or not required fields. like below
It should be either zero(exclusions/ not required) or one(inclusions/required fields.) cheers!!!