【发布时间】:2020-05-20 22:18:45
【问题描述】:
我想检索最多的城市数据,例如仅显示雅加达和贝加西的城市,因为计数城市的数据总数最多。并且可以使用多少动态限制。
链接示例: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"
}
我的期望代码显示获取检索城市的最多数据,仅显示雅加达和贝卡西城市,因为数据总数最多的城市。并且可以使用多少动态限制。:
预期输出
{
"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"
}
【问题讨论】:
-
这个
{ "city": { "Bekasi": 1, "Jakarta": 5 }, "date": "2019-11-27" }怎么有"Bekasi": 1而不是"Bogor": 4?? -
我的意思是我每天而不是每天计算所有“总数”。
-
我仍然对您需要什么以及为什么
2019-11-27很奇怪.. -
请详细阅读上面我的问题...
标签: mongodb mongoose mongodb-query