我刚刚被这个问题困住了。我有两个 Mongoose 模式:
var childrenSchema = mongoose.Schema({
name: {
type: String
},
age: {
type: Number,
min: 0
}
});
var parentSchema = mongoose.Schema({
name : {
type: String
},
children: [childrenSchema]
});
问题是,如何childrenSchema
从每个父文档中获取所有子文档(在本例中为对象)?假设我有一些数据:
var parents = [
{ name: "John Smith",
children: [
{ name: "Peter", age: 2 }, { name: "Margaret", age: 20 }
]},
{ name: "Another Smith",
children: [
{ name: "Martha", age: 10 }, { name: "John", age: 22 }
]}
];
我想在一个查询中检索所有 18 岁以上的孩子。这可能吗?每个答案将不胜感激,谢谢!