mongodb - MongoDb聚合检查对象数组中是否存在id
问题描述
我已经写了这个聚合,它工作正常
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 对象中包含的 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}
}
}
推荐阅读
- reactjs - 是否有使用一个代码库开发 Web 和移动应用程序的框架?
- php - HTTP_X_FORWARDED_FOR 显示 ipv6 和 ipv4
- python - 遍历读取前 N 个值的文件
- vue.js - Vuex 存储数据总是驻留在内存中?
- python - 使用 tf.Dataset 训练的模型进行推理
- java - 使用 Scroll 从 Elasticsearch 检索大型结果需要很长时间
- encryption - ECIES 是公共加密算法吗?
- python - 在 Windows 命令提示符中运行 Python 脚本时如何修复 NameError
- attributes - Weka 命令行属性参数
- java - 获取 Spark Streaming 中消息处理的序号