2012-01-13 58 views
0

我想在Model上运行查询,但只返回查询匹配的嵌入式文档。考虑以下...是否可以从Mongoose中的模型中仅提取嵌入式文档?

var EventSchema = new mongoose.Schema({ 
    typ : { type: String }, 
    meta : { type: String } 
}); 

var DaySchema = new mongoose.Schema({ 
    uid: mongoose.Schema.ObjectId, 
    events: [EventSchema], 

    dateR: { type: Date, 'default': Date.now } 

}); 

function getem() { 
    DayModel.find({events.typ : 'magic'}, function(err, days) { 
     // magic. ideally this would return a list of events rather then days  
    }); 

} 

该查找操作将返回DayModel文档的列表。但是我真正喜欢的是单独的EventSchemas列表。这可能吗?

回答

3

这是不可能的,以获取事件直接对象,但你可以限制哪些字段查询返回这样的:

DayModel.find({events.typ : 'magic'}, ['events'], function(err, days) { 
    ... 
}); 

您仍然需要遍历结果,以提取从实际嵌入式领域但是,查询返回的文档。

相关问题