【问题标题】:How to group by multilevel or multiple mongoDB如何按多级或多个 mongoDB 分组
【发布时间】:2020-02-04 16:39:18
【问题描述】:

如何获得多级分组,如果有数据城市Bekasi和茂物,如何按日期和城市添加分组?

链接示例:MongoPlayground

输入

[
  {
    "_id": ObjectId("5dcbb864e7599115cf1b8cf6"),
    "date": "2019-11-13T15:01:39.040Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bekasi",
      "country": "Indonesia",
      "district": "Jatimulya",
      "latitude": -6.266871666666666,
      "longitude": 107.01972499999998,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-13T15:05:28.391Z"
  },
  {
    "_id": ObjectId("5dcbb949e7599115cf1b9441"),
    "date": "2019-11-13T15:05:28.379Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bekasi",
      "country": "Indonesia",
      "district": "Jatimulya",
      "latitude": -6.266871666666666,
      "longitude": 107.01972499999998,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-13T15:05:28.391Z"
  },
  {
    "_id": ObjectId("5dcbba2ee7599115cf1ba50b"),
    "date": "2019-11-13T15:09:17.001Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bekasi",
      "country": "Indonesia",
      "district": "Jatimulya",
      "latitude": -6.266871666666666,
      "longitude": 107.01972499999998,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-13T15:09:17.009Z"
  },
  {
    "_id": ObjectId("5dcbbb14e7599115cf1bb9ff"),
    "date": "2019-11-13T15:13:06.010Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bekasi",
      "country": "Indonesia",
      "district": "Jatimulya",
      "latitude": -6.266871666666666,
      "longitude": 107.01972499999998,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-13T15:13:06.019Z"
  },
  {
    "_id": ObjectId("5dcf596e8c3dfe4d6e865acf"),
    "date": "2019-11-16T09:05:32.160Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bekasi",
      "country": "Indonesia",
      "district": "Gandasari",
      "latitude": -6.27103,
      "longitude": 107.09003333333334,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-16T09:05:32.175Z",

  },
  {
    "_id": ObjectId("5dcf5a538c3dfe4d6e866a78"),
    "date": "2019-11-16T09:09:21.265Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bekasi",
      "country": "Indonesia",
      "district": "Gandasari",
      "latitude": -6.27103,
      "longitude": 107.09003333333334,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-16T09:09:21.273Z"
  },
  {
    "_id": ObjectId("5dde3eab4144014acda0563b"),
    "date": "2019-11-27T16:15:21.740Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bogor",
      "country": "Indonesia",
      "district": "Cibinong",
      "latitude": -6.498371666666666,
      "longitude": 106.85079166666668,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-27T16:15:21.753Z"
  },
  {
    "_id": ObjectId("5dde3fa14144014acda083ac"),
    "date": "2019-11-27T16:19:27.102Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bogor",
      "country": "Indonesia",
      "district": "Cibinong",
      "latitude": -6.498371666666666,
      "longitude": 106.85079166666668,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-27T16:19:27.114Z"
  },
  {
    "_id": ObjectId("5dde40984144014acda09bb3"),
    "date": "2019-11-27T16:23:32.742Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bogor",
      "country": "Indonesia",
      "district": "Cibinong",
      "latitude": -6.498371666666666,
      "longitude": 106.85079166666668,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-27T16:23:32.753Z"
  },
  {
    "_id": ObjectId("5dde41a64144014acda0bd03"),
    "date": "2019-11-27T16:28:04.557Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bogor",
      "country": "Indonesia",
      "district": "Cibinong",
      "latitude": -6.498371666666666,
      "longitude": 106.85079166666668,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-27T16:28:04.569Z"
  },
  {
    "_id": ObjectId("5dde429b4144014acda0d6a9"),
    "date": "2019-11-27T16:32:09.888Z",
    "id": NumberInt(166),
    "location": {
      "city": "Bekasi",
      "country": "Indonesia",
      "district": "Jatimulya",
      "latitude": -6.266871666666666,
      "longitude": 107.01972499999998,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-27T16:32:09.909Z"
  }
]

实施

db.name_collection.aggregate(
   [
        { "$match": {  
            "update_at": { "$gte": "2019-11-04T00:00:00.0Z", "$lt": "2019-11-06T00:00:00.0Z"},
             "id": { "$in": [166] } 
        } },  
        {
           "$group" : { 
               "_id": {
                       $substr: [ '$update_at', 0, 10 ] 
                },
               "count" : {
                   "$sum" : 1
               } 
           }
        },
        {
            "$project" : {
                "_id" : 0,
                "date" : "$_id",
                "count" : "$count" 
            }
        },
        {
            "$sort" : {
                "date" : 1
            }
        }
   ]
)

实际输出

 {
    "date": "2019-11-13",
    "value": 4
  },
  {
    "date": "2019-11-16",
    "value": 2
  },
  {
    "date": "2019-11-27",
    "value": 5
  }

我的期望代码按日期和城市显示分组,将对象城市添加到结果数组并每天计算值。如何根据现有分组按日期显示城市值的个数,例如如果null会填0: 预期输出

{
    "date": "2019-11-13",
    "value": 4,
    "city" : {
     "bekasi" : 4,
     "bogor" : 0
    }
  },
  {
    "date": "2019-11-16",
    "value": 2,
    "city" : {
     "bekasi" : 2,
     "bogor" : 0
    }
  },
  {
    "date": "2019-11-27",
    "value": 5,
    "city" : {
     "bekasi" : 1,
     "bogor" : 4
    }
  }

【问题讨论】:

标签: mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

这个解决方案对于大数据的性能很差:

db.collection.aggregate([
  {
    "$match": {
      "update_at": {
        "$gte": "2019-11-02T00:00:00.0Z",
        "$lt": "2019-12-06T00:00:00.0Z"
      },
      "id": {
        "$in": [
          166
        ]
      }
    }
  },
  {
    "$group": {
      "_id": {
        "date": {
          $substr: [
            "$update_at",
            0,
            10
          ]
        },
        "city": {
          $cond: [
            "$location",
            "$location.city",
            ""
          ]
        }
      },
      "cityCount": {
        "$sum": {
          $cond: [
            "$location",
            1,
            0
          ]
        }
      }
    }
  },
  {
    "$group": {
      "_id": "$_id.date",
      "cities": {
        "$push": {
          "k": "$_id.city",
          "v": "$cityCount"
        },

      },
      "count": {
        "$sum": "$cityCount"
      }
    }
  },
  {
    "$project": {
      _id: 0,
      date: "$_id",
      value: "$count",
      city: {
        $arrayToObject: "$cities"
      }
    }
  },
  {
    $facet: {
      data: [
        {
          $match: {}
        }
      ],
      distinct: [
        {
          $addFields: {
            city: {
              $objectToArray: "$city"
            }
          }
        },
        {
          $unwind: "$city"
        },
        {
          $group: {
            _id: null,
            city: {
              $addToSet: {
                "k": "$city.k",
                v: 0
              }
            }
          }
        },
        {
          $addFields: {
            city: {
              $arrayToObject: "$city"
            }
          }
        }
      ]
    }
  },
  {
    $unwind: "$data"
  },
  {
    $project: {
      date: "$data.date",
      value: "$data.value",
      city: {
        $mergeObjects: [
          {
            $let: {
              vars: {
                empty_city: {
                  $arrayElemAt: [
                    "$distinct",
                    0
                  ]
                }
              },
              in: "$$empty_city.city"
            }
          },
          "$data.city"
        ]
      }
    }
  },
  {
    "$sort": {
      "date": 1
    }
  }
])

MongoPlayground

【讨论】:

  • 你的代码不是动态数据,数据城怎么越大?我已经更新了我的代码,这个问题几乎完成了。如果 null 为 0,则显示给出数据的其他问题。此链接已更新 mongoplayground.net/p/atxfcorm29q
  • @MiftachHidayatullah 你不能得到这样的结果"bogor" : 0
  • @MiftachHidayatullah 根据您的解决方案更新了我的回复
  • 你更新的代码还是一样的结果,你的示例数据和我的数据不一样。
  • @MiftachHidayatullah 当他们不存在时,你不可能获得 city:0 动态组合结果。
猜你喜欢
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 2021-07-08
相关资源
最近更新 更多