【问题标题】:Mongo Query to fetch distinct nested documentsMongo Query 获取不同的嵌套文档
【发布时间】:2020-08-31 00:36:27
【问题描述】:

我需要获取不同的嵌套文档。

请查找示例文档:

{
    "propertyId": 1001820437,
    "date": ISODate("2020-07-17T00:00:00.000Z"),
    "HList":[
        {
            "productId": 123,
            "name": "Dubai",
            "tsh": true
        }
    ],
    "PList":[
        {
            "productId": 123,
            "name": "Dubai",
            "tsh": false
        },
        {
            "productId": 234,
            "name": "India",
            "tsh": true
        }
    ],
    "CList":[
        {
            "productId": 234,
            "name": "India",
            "tsh": false
        }
    ]
}

预期结果是:

{
    "produts":[
        {
            "productId": 123,
            "name": "Dubai"
        },
        {
            "productId": 234,
            "name": "India"
        }
    ]
}

我试过这个查询:

 db.property.aggregate([
    { 
        $match: { 
            "propertyId": 1001820437,
            "date": ISODate("2020-07-17T00:00:00.000Z")
        }
    },
    { 
        "$project": {
            "_id": 0,
            "unique": {
                "$filter": {    
                    "input": {
                        "$setDifference": [
                            { 
                                "$concatArrays": [
                                    "$HList.productId",
                                    "$PList.productId",
                                    "$CList.productId"
                                ]
                            },
                            []
                        ]  
                    },
                    "cond": { 
                        "$ne": [ "$$this", "" ] 
                    }
                }
            }
        }
    }
]);

$setDifference 聚合在这里是正确的选择吗?
我的查询只返回唯一的产品 ID,但我需要一个 productIdname。 有人可以帮我解决这个问题吗? 提前致谢

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    您可以使用 $projectfirst 删除 tsh 字段,然后运行 ​​$setUnion 忽略重复条目:

    db.collection.aggregate([
        {
            $project: {
                "HList.tsh": 0,
                "PList.tsh": 0,
                "CList.tsh": 0,
            }
        },
        {
            $project: {
                products: {
                    $setUnion: [ "$HList", "$PList", "$CList" ]
                }
            }
        }
    ])
    

    Mongo Playground

    【讨论】:

      【解决方案2】:

      以下两个聚合返回预期的相同结果(您可以使用两者中的任何一个):

      db.collection.aggregate( [
        { 
            $project: { 
                _id: 0, 
                products: {
                    $reduce: {
                        input: { $setUnion: [ "$HList", "$PList", "$CList" ] }, 
                        initialValue: [],
                        in: {
                            $setUnion: [ "$$value", [ { productId: "$$this.productId", name: "$$this.name" } ] ]
                        }
                    } 
                } 
            } 
        }
      ] )
      

      这个有点冗长:

      db.collection.aggregate( [
        { 
            $project: { list: { $setUnion: [ "$HList", "$PList", "$CList" ] } } 
        },
       { 
            $unwind: "$list" 
        },
        { 
            $group: { 
                _id: null, 
                products: { $addToSet: { "productId": "$list.productId", "name": "$list.name" } } 
            } 
        },
        { 
            $project: { _id: 0 } 
        }
      ] )
      

      【讨论】:

        【解决方案3】:
        db.collection.aggregate([
           { 
              $match: { 
                       "propertyId": 1001820437,
                       "date": ISODate("2020-07-17T00:00:00.000Z")
            
                       }
            },
            {
                $project: {
                    products: {
                        $filter: {
                         input: { "$setUnion" : ["$CList", "$HList", "$PList"] },
                         as: 'product',
                         cond: {}
                         }
                    }
                 }
            },
            {
               $project: {            
                           "_id":0,
                           "products.tsh": 1,
                           "products.name": 1,           
                         }
            }, 
        ])
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-28
          • 2013-11-24
          • 2020-09-27
          • 2020-09-12
          • 1970-01-01
          • 1970-01-01
          • 2016-05-20
          相关资源
          最近更新 更多