【问题标题】:Meteor JS $near Reactive SortingMeteor JS $near 反应式排序
【发布时间】:2013-10-28 04:59:11
【问题描述】:

我很高兴看到最近在 Meteor 0.6.6 中的 minimongo 中添加了对地理空间索引的 $near 支持。但是,似乎 $near 的排序行为(它应该按距离排序)是被动的。也就是说,当一个文档被添加到集合中时,客户端会加载它,但总是在结果列表的末尾,即使它比其他文档更接近 $near 坐标。当我刷新页面时,顺序会更正。

例如:

服务器:

Meteor.publish('events', function(currentLocation) {
    return Events.find({loc: {$near:{$geometry:{ type:"Point", coordinates:currentLocation}}, $maxDistance: 2000}});
});

客户:

Template.eventsList.helpers({
    events: function() {
        return Events.find({loc: {$near:{$geometry:{ type:"Point", coordinates:[-122.3943391, 37.7935434]}}, 
$maxDistance: 2000}});
    }
});

有没有办法让它反应排序?

【问题讨论】:

  • 嗨,我在最近的版本中实现了 $near 支持,并实际测试了排序以及其他属性。我会把这个问题放在我的列表中,在 GitHub 上开一张票也很好
  • 这仍然适用于 meeor v1 吗?

标签: mongodb meteor


【解决方案1】:

$near 查询的排序反应性与 minimongo 中的任何其他查询一样,没有什么特别之处:minimongo 使用一些排序功能,要么基于您在查询中传递的排序说明符,要么对包含 $near 运算符的查询进行默认排序。

Minimongo 会对所有内容进行排序,并在每次更新时将之前的订单与新订单进行比较。

从您最初的问题来看,目前尚不清楚您期望什么行为以及您看到了什么。为了证明所提到的排序是反应性的,我编写了一个小应用程序来展示它:

html 模板:

<body>
  {{> hello}}
</body>

<template name="hello">
  Something will go here:
  {{#each things}}
    <p>{{name}}
  {{/each}}
</template>

和JS文件:

C = new Meteor.Collection('things');

if (Meteor.isClient) {
  Template.hello.things = function () {
    return C.find({location:{$near:{$geometry:{type: "Point",coordinates:[0, 0]}, $maxDistance:50000}}});
  };

}

if (Meteor.isServer) {
  Meteor.startup(function () {
    C.remove({});

    var j = 0;
    var x = [10, 2, 4, 3, 9, 1, 5, 4, 3, 1, 9, 11];

    // every 3 seconds insert a point at [i, i] for every i in x.
    var handle = Meteor.setInterval(function() {
      var i = x[j++];
      if (!i) {
        console.log('Done');
        clearInterval(handle);
        return;
      }

      C.insert({
        name: i.toString(),
        location: {
          type: "Point",
          coordinates: [i/1000, i/1000]
        }
      });
    }, 3000);
  });
}

启动应用程序并打开浏览器后立即看到的内容:x 数组中的数字一一出现在屏幕上。每次新号码到达时,它都会出现在正确的位置,始终保持顺序排序。

你所说的“$near 反应式排序”是不是别的意思?

【讨论】:

  • 是的,它有效。我混合了 mongodb 支持的 GeoJSON 点和坐标对,但您对 minimongo 的添加不支持。没有理由支持这一点,我认为 mongodb 也不应该避免混淆。抱歉误报!
  • @islavko,我一直在尝试这样做,但它不起作用..是我格式化数据集的方式还是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
  • 2016-11-24
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
相关资源
最近更新 更多