首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB Aggregation add next document (流星应用)

MongoDB Aggregation add next document (流星应用)
EN

Stack Overflow用户
提问于 2014-11-11 17:02:27
回答 1查看 192关注 0票数 3

下面是我的发布函数,它几乎是Average Aggregation Queries in Meteor的副本

用途:显示文档列表。然后,用户可以单击一个项目(餐厅),并可以查看详细信息。在详细信息中,有上一个和下一个箭头,这将允许循环通过列表。

我的想法是使用聚合方法来用上一个和下一个文档id来丰富每个文档,这样我就可以轻松地构建我的上一个和下一个链接。

但是怎么做呢?我没有头绪。也许有更好的方法?

代码语言:javascript
复制
Meteor.publish("restaurants", function(coords, options) {
    if (coords == undefined) {
        return false;
    }
    if (options == undefined) {
        options = {};
    }
    if (options.num == undefined) {
        options.num = 4
    }
    console.log(options)
    /**
     * the following code is from https://stackoverflow.com/questions/18520567/average-aggregation-queries-in-meteor
     * Awesome piece!!!
     * 
     * */
    var self = this;
    // This works for Meteor 1.0
    var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;

    // Your arguments to Mongo's aggregation. Make these however you want.
    var pipeline = [
        {
         $geoNear: {
            near: { type: "Point", coordinates: [ coords.lng , coords.lat ] },
            distanceField: "dist.calculated",
            includeLocs: "dist.location",
            num: options.num,
            spherical: true
         }
       }
    ];

    db.collection("restaurants").aggregate(        
        pipeline,
        // Need to wrap the callback so it gets called in a Fiber.
        Meteor.bindEnvironment(
            function(err, result) {
                // Add each of the results to the subscription.
                _.each(result, function(e) {
                    e.dist.calculated = Math.round(e.dist.calculated/3959/0.62137*10)/10;
                    self.added("restaurants", e._id, e);
                });
                self.ready();
            },
            function(error) {
                Meteor._debug( "Error doing aggregation: " + error);
            }
        )
    );
});
EN

回答 1

Stack Overflow用户

发布于 2014-12-04 10:59:08

如果你没有太多关于结果的文档,你可以用JavaScript来实现。将您的订阅代码更改为类似这样的代码(我没有测试此代码)。

代码语言:javascript
复制
_.each(result, function(e, idx) {
  if(result[idx - 1]) e.prev = result[idx - 1]._id;
  if(result[idx + 1]) e.next = result[idx + 1]._id;
  e.dist.calculated = Math.round(e.dist.calculated/3959/0.62137*10)/10;
  self.added("restaurants", e._id, e);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26861070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档