【问题标题】:Using ensureIndex for locations in publications in Meteor在 Meteor 的出版物中使用 ensureIndex
【发布时间】:2016-02-14 19:26:02
【问题描述】:

我目前正在尝试仅提取用户附近的集合。我已经能够通过我的其他一些收藏在客户端上成功地做到这一点,但在出版物中很难做到这一点。

我的server/index.js 中有以下代码:

Events._ensureIndex({'loc.coordinates':'2dsphere'});

以下是我的server/publications.js

Meteor.publish('allEventsNearMe', function() {
  if (!this.userId) return null;
  var user = Meteor.users.findOne(this.userId);
  var home = Homes.findOne(user.profile.homeId);
  return Events.find({
    loc: {
      $near: {
        $geometry: {
          type: "Point",
          coordinates: home.loc.coordinates // this is an array ex. [-80, 25]
        },
        $maxDistance:32187
      }
    }
  });
});

当我尝试订阅此出版物时,我收到以下错误:

来自 sub allEventsNearMe id BDspYdPy6BG4RTP42 错误的异常: 轮询查询时出现异常 {"collectionName":"events","selector":{"loc":{"$near":{"$geometry":{"type":"Point","coordinates":[-80.26824199999999,26.123774]} ,"$maxDistance":32187}}},"options":{"transform":null}}: 无法执行查询:错误处理查询:ns=meteor.events

limit=0 skip=0 planner 返回错误:无法找到索引 $geoNear 查询

我尝试在 Meteor.startup 中包装 Events._ensureIndex,但这也没有奏效。有什么想法吗?

【问题讨论】:

    标签: javascript mongodb meteor geospatial


    【解决方案1】:

    我遇到了类似的问题。在 shell 中创建索引并没有解决部署中的问题。只有在我创建了一个模式并将其附加到集合(使用简单模式和集合 2)之后,我才成功在 js 文件中使用 db._ensure 索引并且它工作正常。

    【讨论】:

      【解决方案2】:

      尝试删除当前索引和 create the 2dsphere index,将位置字段指定为键而不是坐标数组。

      要首先删除索引并保留数据,您可以连接到 mongo shell,方法是从应用的根目录(当流星正在运行时)在终端中键入以下内容:

      $ meteor mongo
      

      进入 shell 后,您可以使用以下命令删除索引:

      > db.events.dropIndex({'loc.coordinates':1});
      

      要在文档的“loc”字段上创建“2dsphere”索引,请尝试

      > db.events.ensureIndex({'loc':'2dsphere'});
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-16
        • 1970-01-01
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多