我有兩個集合(items, 和subitems)。該items集合在subItems屬性中具有對子項集合的物件參考(它是子項ObjectId 的陣列)。
我想構建一個查詢,我可以首先填充subItems,然后根據填充的欄位進行過濾。我嘗試運行下面的查詢,但似乎最后運行了 populate,因此subItems[0].sharedGroups.groupId無法正確過濾,因為它尚未填充。
db.items.find({
$or: [
{ 'owner': 1 },
{ 'subItems[0].sharedGroups.groupId': { $in: [3691] } },
]
}).populate('subItems');
uj5u.com熱心網友回復:
你應該使用$lookup然后$match像這樣聚合
db.items.aggregate([
{
$lookup:{
from:"subItems",
localField:"subItems", // field of reference to subItem
foreignField:"_id",
as :"subItems"
}},
{
$match:
{
$or: [
{ 'owner': 1 },
{ 'subItems.sharedGroups.groupId': { $in: [3691] } },
]
}
}]
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/348835.html
標籤:MongoDB 猫鼬 mongodb-查询 猫鼬种群
上一篇:如何在貓鼬中更新陣列中物件的屬性
