Prevent mongoose from return "id" and "_id" field with projection
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" } ]
The id field is a virtual, so you can't project it out. See the docs on the id option:
new bandSchema({ ... }, { id: false });