user762579
user762579

Reputation:

mongoose How to exclude _id from populated

I am populating _group in my role document

return this.find(query, {'_group': 1, 'name':1, 'description': 1} )
  .populate('_group', ['name', 'description'])
  .sort({ createdAt: -1 })
  ...

I get also the _id of the _group

{"_id":"5959ef7db9938a0600f05eb2",
"_group":{
    "_id":"5959ef7db9938a0600f05eae",
    "name":"GroupA",
     "description":"Description GroupA"
 },
"name":"manager",
"description":"can R group, can RW user"},

how can I get rid of the _group._id ?

I tried:

 return this.find(query, {'_group': 1, 'name':1, '_group._id':0, '_id':0} )

but it raises an error : Projection cannot have a mix of inclusion and exclusion.

and

return this.find(query, {'_group': 1, 'name':1, '_id':0, '_id':0}

removes only the role._id

thanks for feedback

Upvotes: 3

Views: 4684

Answers (1)

Prashant Tapase
Prashant Tapase

Reputation: 2147

return this.find(query, {'_group': 1, 'name':1, 'description': 1} ).
    populate({
        path: '_group',
        select: 'name description -_id',
      }).sort({ createdAt: -1 })

Use this logic in your example

Upvotes: 6

Related Questions