使用node.js在mongodb中创建地理空间索引
我在定义地理空间索引“2d”时发现问题,如图所示 以下。关于出了什么问题有任何线索吗?
var Address = new Schema({
loc : {lat: Number, lng: Number },
Address : String,
create_date : {type: Date, default: Date.now}
});
Address.index ({
loc : "2d"
});
它会抛出错误,例如
事件.js:45 抛出参数[1]; // 未处理的“错误”事件 ^ 错误:指向的位置不在范围内 [对象 目的]。 (/cygdrive/c/个人/软件/nodejs/NODE/否 de_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:503:20)
编辑:添加代码
var Address = new Schema({
type : {type: String, enum: ['Apartment', 'House', 'Serviced Apartment'], default: 'Apartment'},
loc : {lat: Number, lng: Number },
Address : String,
create_date : {type: Date, default: Date.now}
});
/*
Address.index ({
loc : "2d"
});
*/
mongoose.connect('mongodb://127.0.0.1:27017/test123', function(err) {
if (err) {
console.log("error in mongo connection");
throw err;
}
console.log("connected to mongo");
});
var RentModel = mongoose.model('Rent', Address);
socket = io.listen(app);
socket.sockets.on('connection', function(client){
console.log('inside on connection');
client.on('register', function(msg){
console.log("msg.geometry.type", msg.geometry.type);
var rent = new RentModel();
rent.type = 'Apartment';
rent.loc.lat = 23;
rent.loc.lng = 56;
rent.Address = "LLLLLLLLIIIIIIIOOOOOOONNNNNNNN"
console.log("before save");
rent.save(function(err){
console.log("rent.save start");
if(err) {
throw err;
console.log("error in save");
}
console.log("saved");
});
});
RentModel.find({loc : { $near : [20, 50], $maxDistance: 30 }} , function(err, docs){
if (err) {
console.log("error in finding near", err);
throw err;
}
console.log('docs.length : ' , docs.length);
console.log('docs : ',docs)
})
I am finding problem in defining the geo spatial index '2d' as shown
below. Any clue as to what is going wrong ?
var Address = new Schema({
loc : {lat: Number, lng: Number },
Address : String,
create_date : {type: Date, default: Date.now}
});
Address.index ({
loc : "2d"
});
It throws error like,
events.js:45
throw arguments[1]; // Unhandled 'error' event
^ Error: point not in range at [object
Object]. (/cygdrive/c/Personal/software/ nodejs/NODE/no
de_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:503:20)
EDIT: added the code
var Address = new Schema({
type : {type: String, enum: ['Apartment', 'House', 'Serviced Apartment'], default: 'Apartment'},
loc : {lat: Number, lng: Number },
Address : String,
create_date : {type: Date, default: Date.now}
});
/*
Address.index ({
loc : "2d"
});
*/
mongoose.connect('mongodb://127.0.0.1:27017/test123', function(err) {
if (err) {
console.log("error in mongo connection");
throw err;
}
console.log("connected to mongo");
});
var RentModel = mongoose.model('Rent', Address);
socket = io.listen(app);
socket.sockets.on('connection', function(client){
console.log('inside on connection');
client.on('register', function(msg){
console.log("msg.geometry.type", msg.geometry.type);
var rent = new RentModel();
rent.type = 'Apartment';
rent.loc.lat = 23;
rent.loc.lng = 56;
rent.Address = "LLLLLLLLIIIIIIIOOOOOOONNNNNNNN"
console.log("before save");
rent.save(function(err){
console.log("rent.save start");
if(err) {
throw err;
console.log("error in save");
}
console.log("saved");
});
});
RentModel.find({loc : { $near : [20, 50], $maxDistance: 30 }} , function(err, docs){
if (err) {
console.log("error in finding near", err);
throw err;
}
console.log('docs.length : ' , docs.length);
console.log('docs : ',docs)
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还值得注意的是,您希望数组中的经度位于纬度之前。当您使用 2D 时,这不会影响您,但当您使用 3D 时,它会影响您。从数学上讲,这是有道理的,因为经度是 X 坐标,纬度是 Y 坐标 (x,y),但我们大多数人都熟悉 lat 很快就会出现(最好的 Mongo 书籍之一有一个 lat 不久之后的例子,但不包括 2D)。
最终,您可能会想要使用 3D,因为当您远离赤道时,2D 计算并不准确。
更新:2015-12-08
上面的内容不再相关 - 请参阅更新的答案/API 文档
It's also worth noting that you will want longitude to come before latitude in your array. This will not affect you when you are using 2D, but it will when you are using 3D. Mathematically this makes sense as longitude is the X coordinate and latitude is the Y coordinate (x,y), but most of us are familiar with lat coming before long (and one of the best Mongo books out there has an example with lat before long, but it does not cover 2D).
Ultimately you're likely going to want to use 3D as 2D calculations are not accurate as you move away from the equator.
UPDATE: 2015-12-08
The above is no longer relevant - please see updated answers/API docs
您可能需要一些关于如何做到这一点的参考:)以及对于后来的人。以防万一,有人感兴趣
http://www.jmanzano.es/blog/?p=第592章
You may want some reference here on how to do it :) And for someone who comes after. Just in case, someone is interested
http://www.jmanzano.es/blog/?p=592