【问题标题】:MongoDB Location NearBy uniqueMongoDB 位置 NearBy 唯一
【发布时间】:2012-08-04 21:09:49
【问题描述】:

我在数据库中创建了一个集合,并使用适当的索引进行了更新以将其取回。我得到以下结果

示例:

db.car.save({ "name":"Toyota car", "affiliation":"Toyota", "loc":{"lon":55.93939251390387,"lat":-113.999}})

db.car.find({"loc" : {"$within" : {"$center" : [[50.93939251390,-114],5]}}})

当我搜索上述查询时,我在数据库中有 100K + 记录返回 20K 记录。

实际上,这在“名称”列中有一些重复值。如何获得“名称”的不同值。

【问题讨论】:

  • 在 Mongodb Geolocation 中使用其他游标来处理数据,因此无法使用 distict、$or、$and。 Map reduce 很慢,添加此功能的票是 1 岁。如果您不需要搜索 $near 只需搜索 loc.0 => '50.93..' 没有 $within。

标签: node.js mongodb mongoose


【解决方案1】:

在 MongoDB 2.0.x 中使用查询无法获得所需的结果,因此需要在应用程序代码中处理结果。

如果您只关注名称,则可以限制输出中的字段:

db.car.find(
    {"loc" : {"$within" : {"$center" : [[50.93939251390,-114],5]}}},
    {"_id": 0, "name": 1}
)

如果你想找到不同的名字,你可以这样做:

// Save cars we've seen
> var cars_seen = {}

// The unique list we actually want
> var cars = []

// Find closest cars to given geo point
> db.car.find(
    {"loc" : {"$within" : {"$center" : [[50.93939251390,-114],5]}}},
    {"_id": 0, "name": 1}
).forEach(
    function(doc) {
        // Add names we haven't seen yet
        if (!cars_seen[doc.name]) {
            cars_seen[doc.name] = 1;
            cars.push(doc.name);
        }
    }
)

> cars
[ "Skoda", "Benz", "Skoda SUV" ]

【讨论】:

    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    相关资源
    最近更新 更多