Reputation: 2998
My User Schema is,
{
_id: String,
name: String,
status: {type: String, "default": "notverified" }
}
In my mongoose query, I need to exclude status
field from the result.
My Model query is like,
User.find({}, {status: false}, function(err, users) {
// process the error
if (err) console.error(err);
// process the result
console.log(users);
});
In the above query, I excluded the status
, but in the result I get documents with the default value which is defined in the schema. i.e status: "notverified"
Even though it is an issue with Mongoose, for quick fix, I tried approaches mentioned here How to exclude some fields from the document.
But it doesn't and work, because that queries returns a single document, so toObject()
will work, but I want to exclude from array of documents, which doesn't have toObject()
method.
Any other possible working solution(before marking it as duplicate)?
Upvotes: 0
Views: 785
Reputation: 153
You misused Mongoose's projection:
User.find({}, {status: 0}, function(err, users) {
// process the error
if (err) console.error(err);
// process the result
console.log(users);
});
Upvotes: 2