Projection does not allow exclusion inclusion together?

In the lecture Shannon states that inclusion and exclusion can be mentioned in the projection and gave an example of _id:0 (it works) but when I tried:
db.movies.find({“cast.0”:“Jeff Bridges”},{title:1,_id:0,genre:0})

it gave me an error:
Error: error: {
“ok” : 0,
“errmsg” : “Projection cannot have a mix of inclusion and exclusion.”,
“code” : 2,
“codeName” : “BadValue”,
“operationTime” : Timestamp(1559754243, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1559754243, 1),
“signature” : {
“hash” : BinData(0,“FUmlxC1PELoV4Kh2GgAOAwwJMSU=”),
“keyId” : NumberLong(“6661714504839069697”)

So is it just the _id0 field that can be excluded together with other inclusions in Projections?

It does not allow the mixing of inclusions and exclusion when returning a result. You must either:

  1. Choose to return results only containing the movie title
  2. Choose to return movies with the id and genres excluded.

The query that you have stated

db.movies.find({“cast.0”:“Jeff Bridges”},{title:1,_id:0,genre:0})

Is equivalent to;

db.movies.find({“cast.0”:“Jeff Bridges”},{title:1})

Though the ID is returned by default…
Actually ID is the only field that you can mix inclusions and exclusions, so this is possible:

 db.movies.find({“cast.0”:“Jeff Bridges”},{_id:0,title:1})

Hi @vinuf666 @Jordan_07066,

Projection allows you to specify or restrict fields to return.
_id is the only field which you need to explicitly exclude in the projection. For all other fields you just need to state the inclusion.

Here, you do not need to add genre:0 as genre field will be excluded unless specified (genre:1). So, if you want only the title field in the output, use the following query:

db.movies.find({“cast.0”:“Jeff Bridges”},{title:1,_id:0})

This query also satisfies both the following conditions:

Here, these queries are not equivalent as the first query will give an error and the second query will display both title and _id field in the result.

For more information on projection, please refer to the following doc:

I hope it help!!

Please let me know, if you have any further doubts.


Thanks everyone guys got it.