我正在尝试仅选择特定字段
exports.someValue = function(req, res, next) {
//query with mongoose
var query = dbSchemas.SomeValue.find({}).select('name');
query.exec(function (err, someValue) {
if (err) return next(err);
res.send(someValue);
});
};
但是在我的 JSON 响应中,我也收到了 _id,而我的文档模式只有两个字段,即 _id 和名称。
[{"_id":70672,"name":"SOME VALUE 1"},{"_id":71327,"name":"SOME VALUE 2"}]
为什么?
.select
只是在获取所有字段后进行筛选,我的建议是使用.find({}, 'name -_id')
。 - hong4rc.find({}, 'name -_id')
看起来无法正常工作? - Shanika Ediriweera.select()
来排除它:.select({“_id”:0})
。 - Oriun