我的用户架构是,
{
_id: String,
name: String,
status: {type: String, "default": "notverified" }
}在我的mongoose查询中,我需要从结果中排除status字段。
我的模型查询如下:
User.find({}, {status: false}, function(err, users) {
// process the error
if (err) console.error(err);
// process the result
console.log(users);
});在上面的查询中,我排除了status,但是在结果中,我得到的文档具有模式中定义的默认值。即status: "notverified"
尽管这是Mongoose的一个问题,但为了快速解决,我尝试了这里提到的How to exclude some fields from the document方法。
但是它不能工作,因为查询返回一个文档,所以toObject()可以工作,但是我想从没有toObject()方法的文档数组中排除它。
是否有其他可行的解决方案(在标记为重复之前)?
发布于 2015-07-08 15:55:53
你误用了Mongoose的投影:
User.find({}, {status: 0}, function(err, users) {
// process the error
if (err) console.error(err);
// process the result
console.log(users);
});https://stackoverflow.com/questions/31286031
复制相似问题