![](/img/trans.png)
[英]MongoDB Aggregate - How to check if a specific field value exists in array of documents
[英]MongoDb aggregate check if id exists in array of objects
我已經寫了這個聚合,它工作正常
db.ParcelStatus.aggregate([{
{
$lookup: {
from: "Parcel",
localField: "parcelId",
foreignField: "_id",
as: "parcel"
}
},
{
$unwind: {
path: "$parcel",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "ParcelStatus",
localField: "parcel._id",
foreignField: "parcelId",
as: "parcel.parcelStatuses"
}
},
{
$lookup: {
from: "Customer",
localField: "parcel.customerData.customerId",
foreignField: "_id",
as: "parcel.customerData.customer"
}
},
{
$unwind: "$parcel.customerData.customer"
}
])
現在在 PARCEL object 中包含的 ParcelStatus 數組中,我需要檢查一下
if(parcel.ParcelStatus.includes((x) => x.statusRepositoryId === 'ID's from frontend')
//then run $match on root against statusRepositoryId === 'SPECIFIC STATIC ID'
我不知道如何在聚合中做到這一點。 您的幫助將不勝感激
只需添加這個$match
階段:
{
$match: {
"parcel.parcelStatuses.statusRepositoryId": {$in: idArrayFromClient}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.