【问题标题】:$lookup is not working after adding limit and skip in aggregate function在聚合函数中添加限制和跳过后,$lookup 不起作用
【发布时间】:2020-03-07 09:31:59
【问题描述】:

我是 mongodb 的新手,我很困惑为什么我的查找在下面的场景中不起作用

// 查找不工作

[
   {
      "$match":{
         "is_active":{
            "$eq":1
         }
      }
   },
   {
      "$facet":{
         "length":[
            {
               "$count":"total"
            }
         ],
         "data":[
            {
               "$skip":0
            },
            {
               "$limit":10
            }
         ]
      }
   },
   {
      "$lookup":{
         "from":"offences",
         "localField":"offences",
         "foreignField":"offence_id",
         "as":"offenceSetail"
      }
   },
   {
      "$project":{
         "offences.is_active":0
      }
   },
   {
      "$replaceRoot":{
         "newRoot":{
            "$mergeObjects":[
               {
                  "$arrayElemAt":[
                     "$offenceSetail",
                     0
                  ]
               },
               "$$ROOT"
            ]
         }
      }
   },
   {
      "$project":{
         "offenceSetail":0
      }
   },
   {
      "$lookup":{
         "from":"registers",
         "localField":"user_id",
         "foreignField":"id",
         "as":"sender"
      }
   },
   {
      "$project":{
         "registers.is_active":0
      }
   },
   {
      "$replaceRoot":{
         "newRoot":{
            "$mergeObjects":[
               {
                  "$arrayElemAt":[
                     "$sender",
                     0
                  ]
               },
               "$$ROOT"
            ]
         }
      }
   },
   {
      "$project":{
         "sender":0
      }
   }
],
"options":{

}
}

//查找正在工作

[
   {
      "$match":{
         "is_active":{
            "$eq":1
         }
      }
   },
   {
      "$lookup":{
         "from":"offences",
         "localField":"offences",
         "foreignField":"offence_id",
         "as":"offenceSetail"
      }
   },
   {
      "$project":{
         "offences.is_active":0
      }
   },
   {
      "$replaceRoot":{
         "newRoot":{
            "$mergeObjects":[
               {
                  "$arrayElemAt":[
                     "$offenceSetail",
                     0
                  ]
               },
               "$$ROOT"
            ]
         }
      }
   },
   {
      "$project":{
         "offenceSetail":0
      }
   },
   {
      "$lookup":{
         "from":"registers",
         "localField":"user_id",
         "foreignField":"id",
         "as":"sender"
      }
   },
   {
      "$project":{
         "registers.is_active":0
      }
   },
   {
      "$replaceRoot":{
         "newRoot":{
            "$mergeObjects":[
               {
                  "$arrayElemAt":[
                     "$sender",
                     0
                  ]
               },
               "$$ROOT"
            ]
         }
      }
   },
   {
      "$project":{
         "sender":0
      }
   },
   {
      "$facet":{
         "length":[
            {
               "$count":"total"
            }
         ],
         "data":[
            {
               "$skip":0
            },
            {
               "$limit":10
            }
         ]
      }
   }
],
"options":{

}
}

请帮助我如何解决它。 谢谢

【问题讨论】:

    标签: mongodb aggregate limit lookup skip


    【解决方案1】:

    第一种情况下的查找不起作用,因为查找中指定的本地字段不存在。在$facet 阶段之后,我们得到两个数组,即lengthdata。我们需要将localField 指定为data.offences

    【讨论】:

      【解决方案2】:

      使用 $facet 您将能够同时运行两个聚合。使用 $facet 获得的每个字段都代表单独的聚合管道,这就是为什么您总是将数组作为输出。

      {
        "$facet":{
           "length":[
              {
                 "$count":"total"
              }
           ],
           "data":[
              { "$skip":0 },
              { "$limit":10 }
           ]
        }
      

      }

      您将获得两个数组,即长度和数据。示例:

      {
      "length" : [ 
          {
              "total" : 360
          }
      ],
      "data" : [ 
          {
              "_id" : ObjectId("5d5beae5662cdb57e2b80eff"),
              "is_active": 1
          }, 
      ...
      ]
      

      }

      所以你必须先 $unwind "data"。然后进行查找操作。所以你可以做这样的事情

      // After $facet
      {
          $unwind: "$data"
      },
      {
            "$lookup":{
               "from":"offences",
               "localField":"data.offences",
               "foreignField":"offence_id",
               "as":"offenceSetail"
            }
       },
      {
          $unwind: "$offenceSetail"
      }
      

      【讨论】:

      • 你好 Ashutosh,我照你说的做了,查找工作正常,但记录总数未获取,但我得到有限记录的总数,即 10
      猜你喜欢
      • 1970-01-01
      • 2021-12-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 2020-08-07
      • 1970-01-01
      • 2016-07-27
      相关资源
      最近更新 更多