使用$ near query

时间:2016-07-25 12:02:00

标签: meteor mongodb-query minimongo

我有一个想要向用户显示的移动应用。每30秒,我想更新近用户列表。在这种情况下,对于这个功能,我没有使用Meteor实时同步,因为我觉得它太重了。我认为每30秒询问一次这个列表会更好。

对于每个User,我有_id和他的mapPosition [lng,lat]。 我的想法是在客户端执行$near查询,因为用户列表应该已经与服务器同步。但是我读到了minimalongo在客户端不支持地理查询。所以我在服务器端创建了一个新方法。 (我仍然没有使用发布/订阅技术)。 问题是我仍然无法正常工作。

用户文档

的示例
var user =
    { _id     : "000000",
     userName : "Daniele",
  mapPosition : { type: "Point",
                  coordinates: [lng, lat]  // float
                } 
    }  

这是我服务器方面的代码

// collections.js
Users      = new Mongo.Collection('users');
Users._ensureIndex({'mapPosition.coordinates':'2dsphere'});



// methods.js
nearUsers(data){

        check(data, 
            {
             mapPosition: [Number], // [lng, lat]
             userId:String          // who is asking
            });

        return Users.find({
                            mapPosition:  { $near: { $geometry: { type: "Point", 
                                                                  coordinates: data.mapPosition 
                                                                }, 
                                                     $maxDistance: 5 *1609.34  // 5 miles in meters
                                                    } 
                                           },
                            '_id' : {$ne: data.userId}
                           }
                          ).fetch();     
}

这是客户端方面的代码

var getNearUsers = function()
  {
    var deferred = $q.defer();

    var mapPosition = [parseFloat(GeolocatorService.getAppPosition().lng), 
                       parseFloat(GeolocatorService.getAppPosition().lat)
                      ];
     Meteor.call('nearUsers', 
                 {
                  userId      : me.id,
                  mapPosition : mapPosition
                 }, 
                 function (err, result) 
                    {
                     if (err) 
                        { 
                         console.error('[getNearUsers] '+err); 
                         deferred.reject(err);   
                        }
                     else
                         {
                           console.log('[getNearUsers]  '+JSON.stringify(result.fetch()));
                          deferred.resolve(result);   
                         }
                    });
      return deferred.promise;
}

// call it each 30 seconds
    setInterval ( function() { getNearUsers(); }, 30000);

在服务器上,我收到错误

Exception while invoking method 'nearUsers' MongoError: Unable to execute query: error processing que$
    at Object.Future.wait (/home/utente/.meteor/packages/meteor-tool/.1.3.2_2.x9uas0++os.linux.x86_32$
    at SynchronousCursor._nextObject (packages/mongo/mongo_driver.js:986:47)
    at SynchronousCursor.forEach (packages/mongo/mongo_driver.js:1020:22)
    at SynchronousCursor.map (packages/mongo/mongo_driver.js:1030:10)
    at SynchronousCursor.fetch (packages/mongo/mongo_driver.js:1054:17)
    at Cursor.(anonymous function) [as fetch] (packages/mongo/mongo_driver.js:869:44)
    at [object Object].nearUsers (server/methods.js:38:47)
    at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1704:12)
    at packages/ddp-server/livedata_server.js:711:19
    at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
    - - - - -
Tree: $and
    $not
        _id == "570a6aae4bd648880834e621"
    lastUpdate $gt 1469447224302.0
    GEONEAR  field=mapPosition maxdist=8046.7 isNearSphere=0
Sort: {}
Proj: {}
 planner returned error: unable to find index for $geoNear query

在客户端上,我收到错误

[Error] Error: [filter:notarray] Expected array but received: {}
http://errors.angularjs.org/1.4.3/filter/notarray?p0=%7B%7D
http://localhost:8100/lib/ionic/js/ionic.bundle.js:13380:32
http://localhost:8100/lib/ionic/js/ionic.bundle.js:31563:31
fn
regularInterceptedExpression@http://localhost:8100/lib/ionic/js/ionic.bundle.js:27539:37
$digest@http://localhost:8100/lib/ionic/js/ionic.bundle.js:28987:43
$apply@http://localhost:8100/lib/ionic/js/ionic.bundle.js:29263:31
tick@http://localhost:8100/lib/ionic/js/ionic.bundle.js:24396:42
    (funzione anonima) (ionic.bundle.js:25642)
    (funzione anonima) (ionic.bundle.js:22421)
    $digest (ionic.bundle.js:29013)
    $apply (ionic.bundle.js:29263)
    tick (ionic.bundle.js:24396)

1 个答案:

答案 0 :(得分:0)

我解决了删除myAppDir / .meteor / local / db文件夹的问题 并重新启动流星