在我的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和title,我会这样做:
Job.find().select("id title").exec(function(err, jobs) {
if (err) throw err;
res.send(jobs);
});但是,我发现这将像预期的那样返回id和title,但它也返回默认的_id字段。那是为什么,我怎么阻止它?
发布于 2015-01-08 12:41:02
在find()函数中,可以传递两个参数(条件和投影)。投影是您想要的字段(或不需要)。在您的情况下,您可以将代码更改为
Job.find({}, {_id:0, id: 1, title: 1}, function(err, jobs) {
if (err) throw err;
res.send(jobs);
});它应该能做到的。
发布于 2019-12-05 17:36:18
https://stackoverflow.com/questions/27840172
复制相似问题