44

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.

4 Answers 4

103

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 })

Or this:

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

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

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

That would also produce an error.

This is all covered in the manual pages.

4
  • 1
    When I want projection list then I have always pass _id:0?
    – Sadikhasan
    Jul 25, 2014 at 6:59
  • @Sadikhasan No. That is just to exclude the "_id" field which is otherwise always included by default. There was a link provided in the example that explains in greater detail. Along with various other tutorial examples that you should spend some time reading.
    – Neil Lunn
    Jul 25, 2014 at 7:01
  • 1
    any idea of why they chose this design? It kinda make sense to allow to be 'too much' verbose in queries
    – Richard
    Sep 21, 2016 at 8:25
  • I received this error because my projection was {id:0, name:1}. I had accidentally deleted the underscore for the ID field's name. It should have been {_id:0, name:1}. I didn't understand the MongoDB terminology "BadValue Projection", but this answer demonstrated to what it referred. May 4, 2017 at 19:45
6

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:

              db.testdata.find({}, {_id:1,name:1})

Or 2) Exclusion:

              db.testdata.find({},{created:0})

In both the above scenarios, it will show only _id and name field.

2
  • Default _id:1 is inclusive always?
    – Sadikhasan
    Jul 25, 2014 at 7:08
  • Yes, 1 is for inclusive. So it is fieldname: inlusion(1)/exclusion(0). Jul 25, 2014 at 7:09
4

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.

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();
});
1
  • 2
    is that a question or an answer?
    – PV8
    Oct 28, 2019 at 9:16
1

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

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!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.