Joe*_*Joe 15 mongoose mongodb node.js
我有这个电话:
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);
});
};
Run Code Online (Sandbox Code Playgroud)
我在哪里指定要返回的属性?在文档中找不到它.以上返回整个对象.我只想要返回一些属性.
这是我的架构:
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);
Run Code Online (Sandbox Code Playgroud)
我不希望我的休息调用中包含items数组.
wdb*_*ley 23
你使用投影.mongoose查询文档中的第一个示例有一个隐藏的投影操作.
NB:不是真正的代码b/c我突出了三星的重要位
// 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.
})
Run Code Online (Sandbox Code Playgroud)
在Person
未指定架构,但我认为这个例子是很清楚.
mar*_*nho 18
MyModel.find({name: "john" }, 'name age address', function(err, docs) { })
Run Code Online (Sandbox Code Playgroud)
这将返回字段 - 仅姓名、年龄和地址。
Has*_*war 15
猫鼬提供了多种项目与文件find
,findOne
和findById
。
// INCLUDE SPECIFIC FIELDS
// find user and return only name and phone fields
User.findOne({ email: email }, 'name phone');
// EXCLUDE SPECIFIC FIELD
// find user and return all fields except password
User.findOne({ email: email }, '-password');
Run Code Online (Sandbox Code Playgroud)
projection
属性进行投影:// find user and return just _id field
User.findOne({ email: email }, {
projection: { _id: 1 }
});
Run Code Online (Sandbox Code Playgroud)
.select
方法:// find user and return just _id and name field
User.findOne({ email: email }).select('name');
// find user and return all fields except _id
User.findOne({ email: email }).select({ _id: 0 });
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23679 次 |
最近记录: |