上午有我节点应用以下猫鼬架构使用猫鼬
var expenseSchema = new Schema({
particular : String,
date : {type : Date, default: Date.now},
paid_by : String,
amount : Number,
month : String
});
var roomSchema = new Schema({
name: String,
admin: String,
roomies : [String],
expenses : [expenseSchema]
},{
collection : 'rooms'
});
我需要费用一房一特定的月份使用find()。 以下是我试图,但它返回我的整个房间对象
Room.findOne({_id : req.params._id, 'expenses.month' : 'oct'}).exec(function(err, result){
if(result == null) {
res.json({result : 'Oops! We couldn\'t find any rooms...'});
} else if(err) {
res.json({result : 'Error in getting Rooms'});
} else {
res.json({result : result});
}
});
有人可以帮助我?
这只返回正确的数据。但它只返回一个对象。我怎样才能获得同一个月的数组中的所有对象? –
这将返回一个空对象 –
我想我知道为什么,这是因为''match'的aggregate()'函数需要通过'_id'匹配,所以必须首先将值转换为ObjectId。检查我的更新版本。 – chridam