Mongodb 是否可以从Mongoose中的模型中仅提取嵌入文档?
我希望在模型上运行查询,但只返回查询匹配的嵌入文档。考虑下面……/P>Mongodb 是否可以从Mongoose中的模型中仅提取嵌入文档?,mongodb,mongoose,Mongodb,Mongoose,我希望在模型上运行查询,但只返回查询匹配的嵌入文档。考虑下面……/P> var EventSchema = new mongoose.Schema({ typ : { type: String }, meta : { type: String } }); var DaySchema = new mongoose.Schema({ uid: mongoose.Schema.ObjectId, events: [EventSchema], dateR: {
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文档的列表。但我真正想要的只是一个事件模式列表。这可能吗?直接获取事件对象是不可能的,但您可以限制查询返回的字段如下:
DayModel.find({events.typ : 'magic'}, ['events'], function(err, days) {
...
});
但是,您仍然需要循环查询结果以从查询返回的文档中提取实际的嵌入字段。无法直接获取事件对象,但您可以限制查询返回的字段如下:
DayModel.find({events.typ : 'magic'}, ['events'], function(err, days) {
...
});
但是,您仍然需要遍历结果,从查询返回的文档中提取实际的嵌入字段