【问题标题】:Mongo Query - match and lookup combinedMongo Query - 匹配和查找相结合
【发布时间】:2021-07-21 10:34:57
【问题描述】:

我定义了以下查询,它会获取所有带有id 的项目,该列表位于ids 的给定列表中,状态为activeretracted

const query = { 
   $and : [ 
     { 
       $or: [
         { 
           status: ‘active’,
         }, 
         { 
           status: ‘retracted’,
         },
       ], 
     }, 
     {
        id: { $in: ids }, 
     }, 
  ],
};

每个项目都有一个parent_id 字段,如果项目没有父项,则该字段可以是null,也可以是父项的id。

我希望我的查询获取所有带有我提供的 ids 的项目,以及它们的父项目(如果存在这样的父项目)。

例如,如果我提供以下 ID

[1,2,3]

并且项目 2 有一个 id 为 5 的父项​​,而项目 1 和 3 的 parent_id 设置为 null,我希望我的查询返回以下项目:

[1,2,3,5].

为此,我编写了以下查询:

const collection = db.collection(‘myCollection’);

const data = await collection.aggregate([ 
      {$match : query}, 
      {
        $lookup: { 
          from: ‘myCollection’, 
          let: { parentID: ‘$parent_id’}, 
          pipeline: [ 
             { 
                $match: { 
                  $expr: { 
                    $eq: [‘$id’, ‘$$parentID’], 
                  }, 
                }, 
             }, 
          as: ‘parent’, 
         }, 
      }, 
     ]).sort(‘created_date’, ‘desc’).toArray();

return data; 

但是,这总是返回 null。

样本数据:

[
{
id: 1, 
parent_id: 3, 
data: ‘bla bla’
}, 
{
id: 2, 
parent_id: null, 
data: ‘bla bla bla’
},
{
id: 3, 
parent_id: null, 
data: ‘bla’
}
]

输入:[1]

输出:

[
{
id: 1, 
parent_id: 3, 
data: ‘bla bla’
}, 
{
id: 3, 
parent_id: null, 
data: ‘bla’
}
]

【问题讨论】:

  • 尝试删除 .toArray()
  • @NikitaMazur 当我删除 toArray 时,我获得了一个具有一堆属性的对象 - 我需要一个结果集,它是一个数组,但可以在我的代码中进行进一步处理
  • @mickl 刚刚添加到问题中

标签: javascript database mongodb mongodb-query nosql


【解决方案1】:

$lookup 在同一个集合上运行的方法应该可以工作,但是它为您提供了一个嵌套数组,因此您需要几个额外的阶段来展平此类数组并获取结果集上的所有元素:

db.collection.aggregate([
    {
        $match: { id: { $in: [1] } }
    },
    {
        $lookup: {
            from: "collection",
            localField: "parent_id",
            foreignField: "id",
            as: "parent"
        }
    },
    {
        $project: {
            all: {
                $concatArrays: [
                    "$parent",
                    [ "$$ROOT" ]
                ]
            }
        }
    },
    {
        $project: {
            "all.parent": 0
        }
    },
    {
        $unwind: "$all"
    },
    {
        $replaceRoot: {
            newRoot: "$all"
        }
    }
])

Mongo Playground

【讨论】:

    【解决方案2】:

    您的聚合格式不正确,缺少一些“]”,例如关闭管道字段。

    如果您修复该查询对我来说可以正常工作。 Example

    【讨论】:

      【解决方案3】:

      你可以试试这个。输入数组是[2,3],其中 2 的父 id=1 并且不在输入数组中。但是输出数组有入口。

      工作Playground

      db.collection.aggregate([
        {
          $match: {
            _id: {
              $in: [
                2,
                3
              ]
            }
          }
        },
        {
          $lookup: {
            from: "collection",
            localField: "p",
            foreignField: "_id",
            as: "parent"
          }
        },
        {
          $project: {
            _id: 0,
            id: {
              $concatArrays: [
                [
                  "$_id"
                ],
                "$parent._id"
              ]
            }
          }
        },
        {
          $unwind: "$id"
        },
        {
          $sort: {
            id: 1
          }
        }
      ])
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 2021-09-05
        • 1970-01-01
        • 2020-06-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多