Mongodb Mongdb geonear查询,按给定字段唯一,按日期获取最新记录
我的收藏(用户位置): 注意:包含重复项Mongodb Mongdb geonear查询,按给定字段唯一,按日期获取最新记录,mongodb,mongodb-query,aggregation-framework,spring-data-mongodb,Mongodb,Mongodb Query,Aggregation Framework,Spring Data Mongodb,我的收藏(用户位置): 注意:包含重复项 [{ "_id": "6098d225376f8417fc6dac65", "userId": 1, "location": { "type": "Point", "coordinates": [ 78.9147, 11.8895 ] }, &quo
[{
"_id": "6098d225376f8417fc6dac65",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9147,
11.8895
]
},
"locationTime": "2021-05-10T06:26:45.101Z",
"createdBy": "admin"
},{
"_id": "6098d243376f8417fc6dac66",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-10T06:27:15.795Z",
"createdBy": "admin"
},{
"_id": "6098d383376f8417fc6dac67",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.6345,
12.5856
]
},
"locationTime": "2021-05-10T06:32:35.297Z",
"createdBy": "admin"
},{
"_id": "6098d46c376f8417fc6dac68",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.6345,
12.5856
]
},
"locationTime": "2021-05-10T06:36:28.338Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}]
任务:基于纬度、经度和距离(公里)。结果应该是唯一的,最新的(按日期)用户ID,并且结果不应该重复。
输入:
纬度:11.8895,
经度:78.9147
距离:50(公里)
我所尝试的:
[{
"_id": "6098d225376f8417fc6dac65",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9147,
11.8895
]
},
"locationTime": "2021-05-10T06:26:45.101Z",
"createdBy": "admin"
},{
"_id": "6098d243376f8417fc6dac66",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-10T06:27:15.795Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}]
db.getCollection('user_location').aggregate([
{
"$sort": {
"userId": 1,
"locationTime": 1
}
},
{
"$group": {
"_id": "$userId",
"locationTime": {
"$last": "$locationTime"
},
"location": {
"$last": "$location"
},
"objectId": {
"$last": "$_id"
},
"createdBy": {
"$last": "$createdBy"
}
}
},
{
"$project": {
"_id": "$objectId",
"userId": "$_id",
"location": 1,
"locationTime": 1,
"createdBy": 1
}
}
])
{
"_id": "6098d383376f8417fc6dac67",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.6345,
12.5856
]
},
"locationTime": "2021-05-10T06:32:35.297Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
查询1以获取最近的数据:
db.getCollection('user_location').aggregate
([
{
"$geoNear": {
"near": [
78.9147,
11.8895
],
"maxDistance": 0.00783927971443699,
"distanceMultiplier": 6378.137,
"spherical": true,
"distanceField": "distance",
"key": "location"
}
}
])
查询1结果(包含重复项):
[{
"_id": "6098d225376f8417fc6dac65",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9147,
11.8895
]
},
"locationTime": "2021-05-10T06:26:45.101Z",
"createdBy": "admin"
},{
"_id": "6098d243376f8417fc6dac66",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-10T06:27:15.795Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}]
db.getCollection('user_location').aggregate([
{
"$sort": {
"userId": 1,
"locationTime": 1
}
},
{
"$group": {
"_id": "$userId",
"locationTime": {
"$last": "$locationTime"
},
"location": {
"$last": "$location"
},
"objectId": {
"$last": "$_id"
},
"createdBy": {
"$last": "$createdBy"
}
}
},
{
"$project": {
"_id": "$objectId",
"userId": "$_id",
"location": 1,
"locationTime": 1,
"createdBy": 1
}
}
])
{
"_id": "6098d383376f8417fc6dac67",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.6345,
12.5856
]
},
"locationTime": "2021-05-10T06:32:35.297Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
查询2以按日期获取最新记录,并按用户ID获取唯一记录:
[{
"_id": "6098d225376f8417fc6dac65",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9147,
11.8895
]
},
"locationTime": "2021-05-10T06:26:45.101Z",
"createdBy": "admin"
},{
"_id": "6098d243376f8417fc6dac66",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-10T06:27:15.795Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}]
db.getCollection('user_location').aggregate([
{
"$sort": {
"userId": 1,
"locationTime": 1
}
},
{
"$group": {
"_id": "$userId",
"locationTime": {
"$last": "$locationTime"
},
"location": {
"$last": "$location"
},
"objectId": {
"$last": "$_id"
},
"createdBy": {
"$last": "$createdBy"
}
}
},
{
"$project": {
"_id": "$objectId",
"userId": "$_id",
"location": 1,
"locationTime": 1,
"createdBy": 1
}
}
])
{
"_id": "6098d383376f8417fc6dac67",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.6345,
12.5856
]
},
"locationTime": "2021-05-10T06:32:35.297Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
查询2结果:
[{
"_id": "6098d225376f8417fc6dac65",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9147,
11.8895
]
},
"locationTime": "2021-05-10T06:26:45.101Z",
"createdBy": "admin"
},{
"_id": "6098d243376f8417fc6dac66",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-10T06:27:15.795Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}]
db.getCollection('user_location').aggregate([
{
"$sort": {
"userId": 1,
"locationTime": 1
}
},
{
"$group": {
"_id": "$userId",
"locationTime": {
"$last": "$locationTime"
},
"location": {
"$last": "$location"
},
"objectId": {
"$last": "$_id"
},
"createdBy": {
"$last": "$createdBy"
}
}
},
{
"$project": {
"_id": "$objectId",
"userId": "$_id",
"location": 1,
"locationTime": 1,
"createdBy": 1
}
}
])
{
"_id": "6098d383376f8417fc6dac67",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.6345,
12.5856
]
},
"locationTime": "2021-05-10T06:32:35.297Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
预期输出:
[{
"_id": "6098d225376f8417fc6dac65",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9147,
11.8895
]
},
"locationTime": "2021-05-10T06:26:45.101Z",
"createdBy": "admin"
},{
"_id": "6098d243376f8417fc6dac66",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-10T06:27:15.795Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}]
db.getCollection('user_location').aggregate([
{
"$sort": {
"userId": 1,
"locationTime": 1
}
},
{
"$group": {
"_id": "$userId",
"locationTime": {
"$last": "$locationTime"
},
"location": {
"$last": "$location"
},
"objectId": {
"$last": "$_id"
},
"createdBy": {
"$last": "$createdBy"
}
}
},
{
"$project": {
"_id": "$objectId",
"userId": "$_id",
"location": 1,
"locationTime": 1,
"createdBy": 1
}
}
])
{
"_id": "6098d383376f8417fc6dac67",
"userId": 2,
"location": {
"type": "Point",
"coordinates": [
78.6345,
12.5856
]
},
"locationTime": "2021-05-10T06:32:35.297Z",
"createdBy": "admin"
},{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
{
"_id": "609b8808d754cc1081e83652",
"userId": 1,
"location": {
"type": "Point",
"coordinates": [
78.9639,
11.7384
]
},
"locationTime": "2021-05-12T07:47:20.521Z",
"createdBy": "admin"
}
以上是预期的输出,因为此记录仅出现在查询1和查询2上。因此,这一次是独一无二的,最新的,在50公里范围内。
问题:如何将两个查询合并为一个查询并获得预期的输出。
临时解决方案:目前,我已经在java上执行了两个查询,并通过id比较它们,然后返回输出。
提前谢谢