【问题标题】:Meteor MongoDb findOne $near not always workingMeteor MongoDb findOne $near 并不总是有效
【发布时间】:2016-10-18 02:59:17
【问题描述】:

我真的不明白。我在 iPhone 上运行的 Meteor (v1.3.3, MongoDb v2.6.7) 应用程序中有以下代码,但它并不总是有效。有时会,有时不会:

Places = new Mongo.Collection("places");

if(Meteor.isServer) {
    Places._ensureIndex({'loc':'2dsphere', name: 1});
}

if(Meteor.isClient) {
    var maxDistance = 50;
    ...
            latLng = Geolocation.latLng();
            if (! latLng)
            {
                console.warn('Could not get location!');
                return;
            }

            console.log('location: ' + latLng.lat + ' (' + typeof latLng.lat  + '), ' + latLng.lng + ' (' + typeof latLng.lng + ')');

            var placeNearby = Places.findOne({
                        loc: {
                            $near: {
                              $geometry: {
                                type: "Point",
                                coordinates: [latLng.lng, latLng.lat]
                              },
                              $maxDistance: maxDistance   //meters
                            }
                        }
                    });
    ...
}

获取 placeNearby 后,我会执行 console.log('placeNearby', placeNearby)。有些时候它确实找到了一个地方,而有些时候它没有。

我通过 Xcode 在我的 iPhone 5s (iOS 9) 上运行它:

meteor run ios-device --mobile-server http://10.0.1.10:3000

当我测试这个时,我总是在同一个地方。我认为这可能是因为 GPS 坐标不精确,所以我将 maxDistance 增加到 50000,但它并没有改变任何东西。有趣的是,我的 console.log 没有为 placeNearby 输出 undefined。它似乎在输出一个空字符串。

但是,如果我在 MongoDb 终端中输入以下查询,它会检索一些结果:

db.places.find({
                 loc: {
                     $near: {
                       $geometry: {
                         type: "Point",
                         coordinates: [-73.56737205923496, 45.50178023952462]
                       },
                       $maxDistance: 50   //meters
                     }
                 }
             })

【问题讨论】:

  • 在 Mongo Shell 中,您正在查找而不获取结果。当你在 JS 中做 findOne 时有结果吗
  • 是的,find 和 findOne 都在 Mongo shell 中返回结果。但我发现了罪魁祸首。看我的回答。

标签: ios mongodb cordova meteor geometry


【解决方案1】:

这是一个愚蠢的错误。我忘记了我在 iPhone 上关闭了 wifi,所以它无法与本地服务器通信。话虽这么说,我不会认为与服务器的连接是必要的。我认为所有数据都被复制到客户端的 minimongo 数据库中,并且应用程序可以独立运行。

我现在想知道是否有一种方法可以发布一个可以在设备上本地运行的 Meteor 应用程序。我阅读了一些有关 GroudDB 的信息,但找不到任何有关使用 $near 和 GroundDB 进行几何查询的文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2015-06-28
    • 2015-12-04
    • 2017-01-08
    • 1970-01-01
    相关资源
    最近更新 更多