3 回答

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個贊
_id除非您明確排除該字段,否則該字段始終存在。使用以下-語法:
exports.someValue = function(req, res, next) {
//query with mongoose
var query = dbSchemas.SomeValue.find({}).select('name -_id');
query.exec(function (err, someValue) {
if (err) return next(err);
res.send(someValue);
});
};
或通過對象顯式:
exports.someValue = function(req, res, next) {
//query with mongoose
var query = dbSchemas.SomeValue.find({}).select({ "name": 1, "_id": 0});
query.exec(function (err, someValue) {
if (err) return next(err);
res.send(someValue);
});
};

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個贊
現(xiàn)在有一種更短的方法:
exports.someValue = function(req, res, next) {
//query with mongoose
dbSchemas.SomeValue.find({}, 'name', function(err, someValue){
if(err) return next(err);
res.send(someValue);
});
//this eliminates the .select() and .exec() methods
};
如果您需要大部分,Schema fields而只想刪除幾個,則可以在字段前面name加上-。例如"-name",第二個參數(shù)中的ex 將不包含name文檔中的字段,而此處給出的示例將僅name包含返回的文檔中的字段。
添加回答
舉報(bào)