我被困在MongoDB查询中.我有两个系列.

plans系列:

{
    planName: 'TV1',
    planFeatures: {
        'AA1': true,
        'AA2 : false',
        'AA3': true,
        'AA4': true,
        'AA5': false
    }
}, {
    planName: 'TV2',
    planFeatures: {
        'AA1': true,
        'AA2 : false',
        'AA3': false,
        'AA4': true,
        'AA5': false
    }
}, ..........

planFeatures个系列

{
    key: 'AA1',
    label: 'ALPHA',
}, {
    key: 'AA2',
    label: 'BETA'
}, ..........

我试图将这两个集合进行聚合,这样planFeatures集合中的key Name字段应该等于planFeatures集合中的关键字段

PaymentPlanFeatures.aggregate([    
            {
                $lookup: {
                    from: 'plans',
                    let: { 'kkey': '$key'},
                    pipeline: [
                        { $set: { 'planFeatures5': { '$objectToArray': '$planFeatures' }}},
                        {
                            $match: {
                                $expr: {
                                    $eq: [
                                       '$planFeatures5.k', '$$kkey'
                                    ]
                                }
                            }
                        }
                    ],
                    as: 'paymentplans'
                }
            }
        ])

我想我的结果应该是这样的

[
    {
        "key": "AA1",
        "label": "ALPHA",
        "paymentplans": [
            {
                "planName": "TV1",
                "planFeatures": {
                    "AA1": true,
                    "AA2": true,
                    "AA3": false
                },
                "__v": 0,
                "planFeatures5": [
                    {
                        "k": "AA1",
                        "v": true
                    }
                ]
            }
        ]
    }, ..............
]

我在这个问题上纠缠了很长时间.

推荐答案

$planFeatures5.k返回字符串数组,您应该使用$in运算符来表示$lookup管道中的$match级.

{
  $in: [
    "$$kkey",
    "$planFeatures5.k"
  ]
}
db.planFeatures.aggregate([
  {
    $lookup: {
      from: "plans",
      let: {
        "kkey": "$key"
      },
      pipeline: [
        {
          $set: {
            "planFeatures5": {
              "$objectToArray": "$planFeatures"
            }
          }
        },
        {
          $match: {
            $expr: {
              $in: [
                "$$kkey",
                "$planFeatures5.k"
              ]
            }
          }
        }
      ],
      as: "paymentplans"
    }
  }
])

Sample Mongo Playground

Mongodb相关问答推荐

用其他集合中的文档替换嵌套文档数组中的值

Mongo DB-如果一个特定字段有多个文档匹配,则更新文档字段

从MongoDB中的嵌套数组中提取找到的值及其索引

如何在Mongo中制作全覆盖索引

MongoDB 在一个聚合中包含多个组

对对象数组进行分组并在 mongodb 中获取计数

Golang 无法在 MongoDB 中创建文档

lexik_jwt_authentication下无法识别的选项api_platform

管道聚合mongodb同一$project阶段的计算字段?

MongoDB 到 Snowflake 连续加载

使用 brew upgrade Mongo update from 3.4 to 4.0 报错:在try 升级到 4.0 之前,数据文件需要完全升级到 3.6 版

如何在 mongodb 本机驱动程序中对 find() 进行字段 Select ?

使用 Node.js 和 mongodb 处理超时

如何在 Mongoose 中更新数组值

MongoDB:自动生成的 ID 为零

遍历所有 Mongo 数据库

mongo - Ruby连接问题

MongoDB mongoexport 查询

我可以使用字符串作为 mongodb 文档的 ID 类型吗?

Mongoose upsert 不创建默认模式属性