【问题标题】:$match value of local field (nested field) not empty string in $lookup pipeline$lookup 管道中本地字段(嵌套字段)的 $match 值不是空字符串
【发布时间】:2019-08-23 05:32:56
【问题描述】:

我不能在查找管道表达式上使用嵌套字段来仅获取非空字符串值。

我正在执行具有 2 个查找阶段的聚合查询,其中第二个查找查询取决于第一个查找结果的嵌套字段。我真的无法弄清楚我的代码有什么问题。

invoice_lines_collection.aggregate([
        {'$match':{'customer':'rtv'}, 
        {'$lookup':{
            'from':'products',
            'localField':'ArticleNumber',
            'foreignField':'number',
            'as':'article_details'
            }
        },
        {$unwind:{
            'path':'$article_details',
            'preserveNullAndEmptyArrays': true
            }
        },
        {'$lookup':{
            'from':'cutomers',
            'let':{'group_id':'$article_details.group._id',
                   'customer':'$customer'},
            'pipeline':{
                {'$unwind':'$productgroups'},
                {'$match':
                      {'$expr':
                            {$and:
                                  ['$ne':['$$group_id','2'],
                                   '$eq': 
                                    ['$productgroups.id','$$group_id'],
                                    '$eq':['$name','$$customer']
                                  ],
                            }
                        }
                 }
             },
             'as':'customer_data'
         }
      }

])

invoice_lines=[
    {
       "_id" : ObjectId("5c885d21a202fc001103saf7"),
       "ShipmentNumber" : "70727320006714asda4",
       "Price" : 179.57,
        "customer" : "test"
    }
]

products = [
             {
               "_id" : ObjectId("2cv21eba4bd009f00161153b7"),
               "number": "1234",
               "group":{
                          "_id" :'',
                          "name" : '',
                  }
             },
             {
                 "_id" : ObjectId("5ca1eba4bd009f00161153b7"),
                 "number" : "2456",
                 "group" : {
                            "_id" : ObjectId("5ca29852bd009f00185553e3"),
                             "name" : "Test group",
                  }
             }
    ]
customers = [
                {
"_id" : ObjectId("5c6fd17a72ef146fcc29c6a1"),
"name" : "test",
"displayName" : "Test",
"productgroups" : [ 
    {
        "name" : "Test group",
        "id" : ObjectId("5ca29852bd009f00185553e3"),
        "markup" : 0.5
    },
     {
        "name" : "Test group 2",
        "id" : ObjectId("5ca29852bd009f0888554443"),
        "markup" : 3.0
    }
    ]
  }
]

我只想拥有一个产品组,并且只获取文章属于某个组的发票行并获取组详细信息。

当我运行上面的代码(转换成 PHP 语言)时,我得到了

表示表达式的对象必须只有一个字段:{ $ne: [ "", "$$group_id" ] ...

【问题讨论】:

    标签: mongodb mongodb-lookup


    【解决方案1】:

    我想通了,这是一些语法错误,最后我不得不做一个 $match 阶段只获取发票行有一个产品链接到产品组的结果,我的代码现在看起来像这样:

    db.getCollection('invoice_lines').aggregate([
        {$match:{'customer':'rtv'}}, 
        {$lookup:{
            'from':'products',
            'localField':'ArticleNumber',
            'foreignField':'number',
            'as':'article_details'
            }
        },
        {$unwind:{
            'path':'$article_details',
            'preserveNullAndEmptyArrays': true
            }
        },
        {$lookup:{
            'from':'customers',
            'let':{'group_id':'$article_details.group._id',
                   'customer':'$customer'},
            'pipeline':[
                {$unwind:'$productgroups'},
                {'$match':
                      {'$expr':
                            {$and:
                                  [
                                    {$ne:['$$group_id','']},
                                    {$eq:['$productgroups.id','$$group_id']},
                                    {$eq:['$name','$$customer']}
                                  ],
                            }
                        }
                 }
             ],
             'as':'customer_data'
         }
    },
     {$match:{'customer_data':{$exists: true, $not: {$size: 0}}}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-09
      • 2019-06-03
      • 2018-08-06
      • 2021-12-12
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多