首页 > mongodb > $ geoNear靠近坐标没有结果

$ geoNear靠近坐标没有结果 ($geoNear no result from near coordinates)

2019-03-04 mongodb

问题

在MongoDB中聚合结果时,我有一种非常奇怪的行为。例如,如果我查询以下内容:

db.getCollection('locations').aggregate([
{"$geoNear": {
    "spherical": true,
    "maxDistance": 14239,
    "near": {
        "type": "Point",
        "coordinates": [45.180584858570136,5.760955810546876]
    },
    "distanceField": "distance"
}},
{"$match": {
    "datetime": {
        "$gte": ISODate("2019-03-01T00:00:00Z"),
        "$lt": ISODate("2019-03-02T00:00:00Z")
    }
}}
])

我没有结果。但是,如果我将坐标改为[45.180584858570136,5.7602691650390625]距离第一点53.82米的位置,我会得到16个像这样的结果:

{
    "_id" : ObjectId("5c791c276bc27675f3bd2d7f"),
    "user" : ObjectId("5c4620c96bc27618b1a39cfe"),
    "coordinates" : {
        "type" : "Point",
        "coordinates" : [ 
            45.1837952, 
            5.7204736
        ]
    },
    "datetime" : ISODate("2019-03-01T11:37:04.000Z"),
    "_updated" : ISODate("2019-03-01T11:48:55.000Z"),
    "_created" : ISODate("2019-03-01T11:48:55.000Z"),
    "_etag" : "535e09d3d25f0b970fef8e45f220e41a99fd11f6",
    "distance" : 4444.24394629098
}

好吧,我无法理解为什么在第一种情况下我没有得到任何结果,因为所有16个结果必须与给定范围匹配...

这是一个更完整的数据样本:https//gist.github.com/sylvainbx/502515b809341173e0ee36d4579ba31b

有没有人之前已经看过这个或者对如何解决这个问题有任何想法?

解决方法

好的,我自己找到了解决方案。更改查询,如下所示可以解决问题:

db.getCollection('locations').aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [45.180584858570136,5.760955810546876] },
        key: "coordinates",
        spherical: true,
        distanceField: "distance",
        maxDistance: 14239,
        query: { 
            "datetime": {
                "$gte": ISODate("2019-03-01T00:00:00Z"),
                "$lt": ISODate("2019-03-02T00:00:00Z")
            }
        }
     }
   }
])

问题

I have a very strange behavior when aggregating results in MongoDB. For example, if I query the following :

db.getCollection('locations').aggregate([
{"$geoNear": {
    "spherical": true,
    "maxDistance": 14239,
    "near": {
        "type": "Point",
        "coordinates": [45.180584858570136,5.760955810546876]
    },
    "distanceField": "distance"
}},
{"$match": {
    "datetime": {
        "$gte": ISODate("2019-03-01T00:00:00Z"),
        "$lt": ISODate("2019-03-02T00:00:00Z")
    }
}}
])

I get no results. But if I change the coordinates to [45.180584858570136,5.7602691650390625] which is 53.82 meters away from the first point, I get 16 results like this one :

{
    "_id" : ObjectId("5c791c276bc27675f3bd2d7f"),
    "user" : ObjectId("5c4620c96bc27618b1a39cfe"),
    "coordinates" : {
        "type" : "Point",
        "coordinates" : [ 
            45.1837952, 
            5.7204736
        ]
    },
    "datetime" : ISODate("2019-03-01T11:37:04.000Z"),
    "_updated" : ISODate("2019-03-01T11:48:55.000Z"),
    "_created" : ISODate("2019-03-01T11:48:55.000Z"),
    "_etag" : "535e09d3d25f0b970fef8e45f220e41a99fd11f6",
    "distance" : 4444.24394629098
}

Well, I can't understand why I get no results in the first case, because all the 16 results must match the given range...

Here's a more complete sample of data : https://gist.github.com/sylvainbx/502515b809341173e0ee36d4579ba31b

Does anyone has already saw this before or has any idea on how to solve this?

解决方法

Ok, I've found the solution by myself. Changing the query as the following fixes the issue:

db.getCollection('locations').aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [45.180584858570136,5.760955810546876] },
        key: "coordinates",
        spherical: true,
        distanceField: "distance",
        maxDistance: 14239,
        query: { 
            "datetime": {
                "$gte": ISODate("2019-03-01T00:00:00Z"),
                "$lt": ISODate("2019-03-02T00:00:00Z")
            }
        }
     }
   }
])
相似信息