Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 返回结果并订阅Meteor Mongo Geosearch_Mongodb_Meteor - Fatal编程技术网

Mongodb 返回结果并订阅Meteor Mongo Geosearch

Mongodb 返回结果并订阅Meteor Mongo Geosearch,mongodb,meteor,Mongodb,Meteor,使用Meteor.js,我已经为我的酒吧/酒吧完美地工作了以下代码。我能够毫无问题地传递参数并返回游标 我的目标是显示当前用户位置和数据库结果之间的距离 由于mongodb已经计算了获得结果集的距离,我不想在其他地方再次计算它。我想返回$geoNear的geoNear.results[n].dis结果,但无法找到一种切实可行的方法。我很感激发布只会将光标返回到文档,但我想知道是否有某种方式可以附加结果 Meteor.publishComposite("public", function(loca

使用Meteor.js,我已经为我的酒吧/酒吧完美地工作了以下代码。我能够毫无问题地传递参数并返回游标

我的目标是显示当前用户位置和数据库结果之间的距离

由于mongodb已经计算了获得结果集的距离,我不想在其他地方再次计算它。我想返回$geoNear的geoNear.results[n].dis结果,但无法找到一种切实可行的方法。我很感激发布只会将光标返回到文档,但我想知道是否有某种方式可以附加结果

Meteor.publishComposite("public", function(location, distance) {
return {
find: function() {
      return Tutors.find({},
                  {
                    $geoNear: {
                        $geometry: {
                          type: "Point" ,
                          coordinates: [ location.lng , location.lat ]
                        },
                        $maxDistance: distance,
                      },
                    }
                  );
                }
}
});

我的subscribe参数只是一个lat/lng对象和以米为单位的距离

如果我告诉你你可以使用Mongo怎么办?这里的总体思路是获得当前用户位置和数据库结果之间的距离,以便随着
'Tutors'
集合的更改而自动更新,因此使用和观察来实现这一点

这是设置。第一步是获取聚合框架包,它为您封装了一些Mongo方法。只要
meteor添加meteohacks:aggregate
,你就应该回家了。这将为您的集合添加一个方法

添加聚合框架支持的另一种方法是直接调用mongoDB并访问底层收集方法,在本例中,您需要方法。因此,在mongoDB中使用此连接:

var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db,
    Tutors = db.collection("tutors");
现在,您可以深入到聚合框架并构建管道查询。下面的示例演示如何在发布中获取聚合,并在发布中使用。这遵循meteor文档中的“房间计数”。通过,您可以知道是否添加、更改或删除了新位置。为简便起见,每次重新运行聚合(删除除外),如果该位置以前已发布,则发布,如果该位置已删除,则从发布中删除该位置,然后对于新位置使用

Meteor.publish('findNearestTutors', function(opts) {
    let initializing = 1, run = (action) => {
        // Define the aggregation pipeline
        let pipeline = [
            {
                $geoNear: {
                    near: {type: 'Point', coordinates: [Number(opts.lng), Number(opts.lat)]},
                    distanceField: 'distance',
                    maxDistance: opts.distance,
                    spherical: true,
                    sort: -1
                }
            }
        ]
        Tutors.aggregate(pipeline).forEach((location) => {
            // Add each of the results to the subscription.
            this[action]('nearest-locations', location._id, location)
            this.ready()
        })
    }

    // Run the aggregation initially to add some data to your aggregation collection
    run('added')

    // Track any changes on the collection you are going to use for aggregation
    let handle = Tutors.find({}).observeChanges({
        added(id) {
          // observeChanges only returns after the initial `added` callbacks
          // have run. Until then, you don't want to send a lot of
          // `self.changed()` messages - hence tracking the
          // `initializing` state.
          if (initializing && initializing--)
            run('changed')
        },
        removed(id) {
            run('changed')
        },
        changed(id) {
            run('changed')
        },
        error(err) {
            throw new Meteor.Error("Houston, we've got a problem here!", err.message)
        }
    })

    // Stop observing the cursor when client unsubs.
    // Stopping a subscription automatically takes
    // care of sending the client any removed messages.
    this.onStop(function () {
        handle.stop();
    })
})

令人惊叹的。你搞定了!