我试图返回由populate
过滤的结果集合的架构
{
myObjects: [{
obj: { type: Schema.ObjectId, ref: myObject }
}]
}
myObject的架构
{
name: { type: String },
active: { type: Boolean }
}
所以
.find().populate('myObjects.obj')
这样会返回结果
[
{ myObjects:
[
{ obj: { name: 'Adam', active: true } },
{ obj: { name: 'Stacy', active: false } },
{ obj: { name: 'James', active: false } }
]
},
{ myObjects:
[
{ obj: { name: 'James', active: false } }
]
},
{ myObjects:
[
{ obj: { name: 'Adam', active: true } }
]
},
{ myObjects:
[
{ obj: { name: 'Adam', active: true } },
{ obj: { name: 'James', active: false } }
]
}
]
此时,我想说"只显示那些活跃的人:真"
所以我做......
.find().populate({ path: 'myObjects.obj', match: { active: true })
除此之外还返回以下内容
[
{ myObjects:
[
{ obj: { name: 'Adam', active: true } }
]
},
{ myObjects:
[
]
},
{ myObjects:
[
{ obj: { name: 'Adam', active: true } }
]
},
{ myObjects:
[
{ obj: { name: 'Adam', active: true } } }
]
}
]
有一个包含空数组的myObjects。如何从结果中排除这一点,以便我只收回
[
{ myObjects:
[
{ obj: { name: 'Adam', active: true } }
]
},
{ myObjects:
[
{ obj: { name: 'Adam', active: true } }
]
},
{ myObjects:
[
{ obj: { name: 'Adam', active: true } } }
]
}
]