MongoServerError: Cannot do exclusion on field date in inclusion projection
Asked Answered
H

4

11

I was working with the MongoDB Atlas Server... and encountered this error... What does it mean...? Can someone explain in simple words plz...

This was the query i was trying...

db.posts.find({}, {title: 1, date: 0})

The structure of the posts in database is as follows:

[
  {
    _id: ObjectId("63739044de169f6d0h2e6a3d"),
    title: 'Post 2',
    body: 'a news post',
    category: 'News',
    likes: 1,
    tags: [ 'news', 'events' ],
    date: 'Tue Nov 15 2022 18:53:24 GMT+0530 (India Standard Time)'
  },
  {
    _id: ObjectId("63739271de179f5d0e31e5b2"),
    title: 'Post 1',
    body: 'hey there, hemant here',
    category: 'random',
    likes: 1,
    tags: [ 'random', 'events' ],
    date: 'Tue Nov 15 2022 18:41:24 GMT+0530 (India Standard Time)'
  }
]

But I got an error which says...

MongoServerError: Cannot do exclusion on field date in inclusion projection

I was trying to get all the document objects excluding the date parameter and including the title parameter but got an error...

Hartzell answered 15/11, 2022 at 15:13 Comment(1)
db.posts.find({}, {title: 1}) Just run this to get the title alone. Rest all fields will be excluded.Fricandeau
P
13

According to the documentation, first argument in find is filter and second is projection. projection allows you to specify 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. You will have to follow the below format.

db.posts.find({}, {title: 1, body:1, category:1, likes:1, tags:1})
Prim answered 15/11, 2022 at 15:36 Comment(0)
S
6

This is a simple error to fix, and I will show you how. You need to understand how projection works. But first, let's look at how the find() method works. The find() method can take two parameters, which are 1. filter and 2. projection. There are two types of projection: 1. inclusion projection and 2. exclusion projection.

  1. Inclusion projection means specifying all fields to be included in result of querying a document. This is done by assigning 1 to all values of keys desired for inclusion in an object that is passed as the second parameter of the find method. Note that only the "_id" key can be added for exclusion when performing inclusion projection.

You can do this to perform inclusion projection for the title field on the post collection:

db.posts.find({}, {"title": 1})

or

db.posts.find({}, {"title": 1, "_id": 0})

and the result will be:

[
  {
    title: 'Post 1'
  },
  {
    title: 'Post 2'
  }
]
  1. Exclusion projection means specifying all fields to be excluded in result of querying a document. This is done by assigning 0 to all values of keys desired for excluded in an object that is passed as the second parameter of the find method. To achieve the same result as the inclusion projection above using the exclusion projection, you can do this:
db.posts.find({}, {"_id": 0, "body":0, "category":0, "likes":0, "tags":0, "date":0})

and the result will be:

[
  {
    title: 'Post 1'
  },
  {
    title: 'Post 2'
  }
]

Thank you for reading. I hope this gives you a deep understanding of projection.

Schizogenesis answered 5/7, 2023 at 4:30 Comment(0)
B
2

Another possibility is that

Inside the 'posts' mongoose schema, if we set the date key like this

date: {
  type: string,
  select: true
}

the select:true always returns the date key in all find queries

and passing {date:0} in projection or dot_select(.select) object will return this error:

Cannot do exclusion on field date in inclusion projection

Bust answered 16/3, 2023 at 9:13 Comment(1)
This hint is very useful! Another property in the schema that may lead to the OPs error is when you make use of required: trueGoltz
C
0

This is a lot late, but here's something that might help you to get all except the date:

db.posts.find({}, {date: 0})
Cofsky answered 10/9, 2023 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.