在我的Mongoose模式中,我有一个id
字段,每个文档都有一个唯一的ID。这是使用与默认_id
字段相同的系统运行的,如下所示:
var JobSchema = new mongoose.Schema({
id: { type:String, required:true, unique:true, index:true, default:mongoose.Types.ObjectId },
title: { type: String },
brief: { type: String }
});
module.exports = mongoose.model("Job", JobSchema);
现在,如果我查询模式获取id和标题,我会这样做:
Job.find().select("id title").exec(function(err, jobs) {
if (err) throw err;
res.send(jobs);
});
然而,我发现这会按预期返回
id
和title
,但它也会返回默认的_id
字段。为什么会这样,如何阻止它?
select
调用中添加-_id
,这样就变成了Job.find().select("-_id id title")
。 - JohnnyHK{_id:0, id: 1}
时,投影正确地仅包括"id"字段,但它被设置为null?当我将其更改为{_id:1, id: 1}
时,“id”属性显示了正确的值,但我当然不想在投影中包含_id字段。奇怪的行为。 - chrismacp