mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Prevent mongoose from return "id" and "_id" field with projection

Open Enado95 opened this issue 7 years ago • 1 comments

I have the following code and I'm trying to prevent the id field from being returned. However, it is still being returned. Is it a case that cannot be excluded?

const docs = await Band.find().select({id: 0}).populate({ path: 'members', select: 'name' });

See results below:

[ { "_id": "5cfe8f8a9a6c2b300c160ece", "name": 1, "__v": 0, "members": { "_id": "5cfe8f8a9a6c2b300c160eca", "name": "Axl Rose", "band": 1 }, "id": "5cfe8f8a9a6c2b300c160ece" }, { "_id": "5cfe8f8a9a6c2b300c160ecf", "name": 2, "__v": 0, "members": { "_id": "5cfe8f8a9a6c2b300c160ecc", "name": "Vince Neil", "band": 2 }, "id": "5cfe8f8a9a6c2b300c160ecf" } ]

Enado95 avatar Jun 10 '19 17:06 Enado95

The id field is a virtual, so you can't project it out. See the docs on the id option:

new bandSchema({ ... }, { id: false });

vkarpov15 avatar Jun 16 '19 18:06 vkarpov15