Mongoose,查找,返回特定属性 [英] Mongoose, find, return specific properties
本文介绍了Mongoose,查找,返回特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我接到了这个电话:
exports.getBIMFromProject = function(req, res){
mongoose.model('bim').find({projectId: req.params['prj_id']}, function(err, bim){
if(err){
console.error(err);
res.send(500)
}
res.send(200, bim);
});
};
在哪里指定要返回的属性?在文档中找不到它.以上返回整个对象.我只想返回一些属性.
Where do I specify which properties I want to return? Can't find it in the docs. The above returns the entire object. I only want a few properties returned.
这是我的架构:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var bimSchema = new Schema({
projectId: Number,
user: String,
items:[
{
bimObjectId: Number,
typeId: String,
position:{
floor: String,
room:{
name: String,
number: String
}
}
}
]
});
mongoose.model('bim', bimSchema);
我不希望在我的 rest 调用中包含 items 数组.
I don't want the items array included in my rest call.
推荐答案
您使用投影.mongoose 查询文档 中的第一个示例包含一个投影操作.
You use projection. The first example in the mongoose query docs has a projection operation tucked in.
注意:不是真正的代码 b/c 我用三颗星突出了重要的部分
NB: not real code b/c I highlighted the important bits with triple stars
// find each person with a last name matching 'Ghost', ***selecting the `name` and `occupation` fields***
Person.findOne({ 'name.last': 'Ghost' }, ***'name occupation'***, function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host.
})
没有指定 Person
架构,但我认为这个例子已经足够清楚了.
The Person
schema isn't specified but I think the example is clear enough.
这篇关于Mongoose,查找,返回特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文