Как установить ограничение в группе объектов массива по счету MongoDB - PullRequest
0 голосов
/ 05 февраля 2020

Я хочу получить большинство данных города, например, будет показан только город Джакарта и Бекаси, потому что наибольшее количество данных в городе считается. и может использовать сколько динамически ограничений.

Пример ссылки: MongoPlayground

Вход

[
  {
    "_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"
  },
  {
    "_id": ObjectId("5ccbb96706d1d47a4b2ced4b"),
    "date": "2019-11-13T15:05:28.379Z",
    "id": NumberInt(166),
    "location": {
      "city": "Jakarta",
      "country": "Indonesia",
      "district": "Jatimulya",
      "latitude": -6.266871666666666,
      "longitude": 107.01972499999998,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-13T15:05:28.391Z"
  },
  {
    "_id": ObjectId("5ccbb96706d1d47a4b2ced4c"),
    "date": "2019-11-13T15:09:17.001Z",
    "id": NumberInt(166),
    "location": {
      "city": "Jakarta",
      "country": "Indonesia",
      "district": "Jatimulya",
      "latitude": -6.266871666666666,
      "longitude": 107.01972499999998,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-13T15:09:17.009Z"
  },
  {
    "_id": ObjectId("5ccbb96706d1d47a4b2ced4d"),
    "date": "2019-11-13T15:13:06.010Z",
    "id": NumberInt(166),
    "location": {
      "city": "Jakarta",
      "country": "Indonesia",
      "district": "Jatimulya",
      "latitude": -6.266871666666666,
      "longitude": 107.01972499999998,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-13T15:13:06.019Z"
  },
  {
    "_id": ObjectId("5ccbb96706d1d47a4b2ced4e"),
    "date": "2019-11-16T09:05:32.160Z",
    "id": NumberInt(166),
    "location": {
      "city": "Jakarta",
      "country": "Indonesia",
      "district": "Gandasari",
      "latitude": -6.27103,
      "longitude": 107.09003333333334,
      "province": "Jawa Barat"
    },
    "update_at": "2019-11-16T09:05:32.175Z",

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

Реализация

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", 
      city: {
        $mergeObjects: [
          {
            $let: {
              vars: {
                empty_city: {
                  $arrayElemAt: [
                    "$distinct",
                    0
                  ]
                }
              },
              in: "$$empty_city.city"
            }
          },
          "$data.city"
        ]
      }
    }
  },
  {
    "$sort": {
      "date": 1
    }
  }
])

Фактический результат :

 {
    "city": {
      "Bekasi": 3,
      "Bogor": 0,
      "Jakarta": 3
    },
    "date": "2019-11-13" 
  },
  {
    "city": {
      "Bekasi": 2,
      "Bogor": 0,
      "Jakarta": 2
    },
    "date": "2019-11-16" 
  },
  {
    "city": {
      "Bekasi": 1,
      "Bogor": 4,
      "Jakarta": 5
    },
    "date": "2019-11-27" 
  }

Мой код ожидания показывает, чтобы получить большинство данных города, только город Джакарта и Бекаси будет быть показанным, потому что наибольшее количество данных подсчитывает город. и может использовать сколько динамически ограничения.:

1023 * Ожидаемый выход
 {
    "city": {
      "Bekasi": 3, 
      "Jakarta": 3
    },
    "date": "2019-11-13" 
  },
  {
    "city": {
      "Bekasi": 2, 
      "Jakarta": 2
    },
    "date": "2019-11-16" 
  },
  {
    "city": {
      "Bekasi": 1, 
      "Jakarta": 5
    },
    "date": "2019-11-27" 
  }

1 Ответ

0 голосов
/ 05 февраля 2020

На самом деле я проверил ваш код, есть ошибка при добавлении $$empty_city.city, поэтому удалите один знак $ ($empty_city.city) и запустите код ниже.

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",
      city: {
        $mergeObjects: [
          {
            $let: {
              vars: {
                empty_city: {
                  $arrayElemAt: [
                    "$distinct",
                    0
                  ]
                }
              },
              in: "$empty_city.city"
            }
          },
          "$data.city"
        ]
      }
    }
  },
  {
    "$sort": {
      "date": 1
    }
  }
])

, чтобы вы получили фактический вывод как ниже

[
  {
    "city": {
      "Bekasi": 3,
      "Jakarta": 3
    },
    "date": "2019-11-13"
  },
  {
    "city": {
      "Bekasi": 2,
      "Jakarta": 2
    },
    "date": "2019-11-16"
  },
  {
    "city": {
      "Bekasi": 1,
      "Bogor": 4,
      "Jakarta": 5
    },
    "date": "2019-11-27"
  }
]
...