Meteor JS $near 反应式排序

3
我很高兴地看到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上开一个票。 - imslavko
这个还能在 Meeor v1 上使用吗? - Sahan
1个回答

7

$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反应式排序”是否指其他内容?


是的,它可以工作。我混合了GeoJSON点和坐标对,这是mongodb支持的,但你添加到minimongo的不支持。没有理由支持这个,我认为mongodb也不应该支持以避免混淆。对于错误警报感到抱歉! - Adam Berlinsky-Schine
@islavko,我尝试这样做很久了,但它不起作用...是因为我的数据集格式有问题还是其他原因? - Sahan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接