在 Meteor 的出版物中使用 ensureIndex 定位
Using ensureIndex for locations in publications in Meteor
我目前正在尝试仅拉取用户附近的 collections。我已经能够与我的其他一些 collections 在客户端上成功地做到这一点,但我正在努力在出版物中完成它。
我的 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
}
}
});
});
当我尝试订阅此出版物时,出现以下错误:
Exception from sub allEventsNearMe id BDspYdPy6BG4RTP42 Error:
Exception while polling query
{"collectionName":"events","selector":{"loc":{"$near":{"$geometry":{"type":"Point","coordinates":[-80.26824199999999,26.123774]},"$maxDistance":32187}}},"options":{"transform":null}}:
Unable to execute query: error processing query: ns=meteor.events
limit=0 skip=0 planner returned error: unable to find index for
$geoNear query
我试过将 Events._ensureIndex 包装在 Meteor.startup 中,但这也没有用。有什么想法吗?
尝试删除当前索引和 create the 2dsphere index,将位置字段指定为键而不是坐标数组。
要首先删除索引并保留您的数据,您可以连接到 mongo shell,方法是从您的应用程序的根目录在您的终端中输入以下内容(而 meteor 是 运行):
$ meteor mongo
进入 shell 后,您可以使用此命令删除索引:
> db.events.dropIndex({'loc.coordinates':1});
要在文档的 "loc" 字段上创建“2dsphere”索引,请尝试
> db.events.ensureIndex({'loc':'2dsphere'});
我遇到了类似的问题。在 shell 中创建索引没有解决部署中的问题。只有在我创建了一个模式并将其附加到集合(使用简单模式和集合 2)之后,我才成功地在 js 文件中使用 db._ensure 索引并且它工作正常。
我目前正在尝试仅拉取用户附近的 collections。我已经能够与我的其他一些 collections 在客户端上成功地做到这一点,但我正在努力在出版物中完成它。
我的 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
}
}
});
});
当我尝试订阅此出版物时,出现以下错误:
Exception from sub allEventsNearMe id BDspYdPy6BG4RTP42 Error: Exception while polling query {"collectionName":"events","selector":{"loc":{"$near":{"$geometry":{"type":"Point","coordinates":[-80.26824199999999,26.123774]},"$maxDistance":32187}}},"options":{"transform":null}}: Unable to execute query: error processing query: ns=meteor.events
limit=0 skip=0 planner returned error: unable to find index for $geoNear query
我试过将 Events._ensureIndex 包装在 Meteor.startup 中,但这也没有用。有什么想法吗?
尝试删除当前索引和 create the 2dsphere index,将位置字段指定为键而不是坐标数组。
要首先删除索引并保留您的数据,您可以连接到 mongo shell,方法是从您的应用程序的根目录在您的终端中输入以下内容(而 meteor 是 运行):
$ meteor mongo
进入 shell 后,您可以使用此命令删除索引:
> db.events.dropIndex({'loc.coordinates':1});
要在文档的 "loc" 字段上创建“2dsphere”索引,请尝试
> db.events.ensureIndex({'loc':'2dsphere'});
我遇到了类似的问题。在 shell 中创建索引没有解决部署中的问题。只有在我创建了一个模式并将其附加到集合(使用简单模式和集合 2)之后,我才成功地在 js 文件中使用 db._ensure 索引并且它工作正常。