我一直在做这样一件直截了当的事情。我只想使用 nodejs、mongoose、restify 堆栈对用户模型进行 CRUD 操作。我的 mongo 实例在 mongolab 上。 用户应该包含一个“loc”字段。用户架构如下:
var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var userSchema = new Schema( {
email_id : { type: String, unique: true },
password: { type: String},
first_name: String,
last_name: String,
age: String,
phone_number: String,
profile_picture: String,
loc: {
type: {},
coordinates: [Number]
}
});
userSchema.index({loc:'2d'});
var User = mongoose.model('user', userSchema);
module.exports = User;
其余用来发帖的api如下:
create_user : function (req, res, next) {
var coords = [];
coords[0] = req.query.longitude;
coords[1] = req.query.latitude;
var user = new User(
{
email_id : req.params.email_id,
password: req.params.password,
first_name: req.params.first_name,
last_name: req.params.last_name,
age: req.params.age,
phone_number: req.params.phone_number,
profile_picture: req.params.profile_picture,
loc: {
type:"Point",
coordinates: [1.0,2.0] // hardcoded just for demo
}
}
);
user.save(function(err){
if (err) {
res.send({'error' : err});
}
res.send(user);
});
return next();
},
现在,当我在 curl -X POST http://localhost:3000/user --data "email_id=sdass@dfAadsfds&last_name=dass&age=28&phone_number=123456789&profile_picture=www.jakljf.com&longitude=1.0 上进行 POST 调用时&latitude=2.0"
我收到以下错误
{
error: {
code: 16804
index: 0
errmsg: "insertDocument :: caused by :: 16804 location object expected, location array not in correct format"
op: {
email_id: "sdass@dfAadsfdsadkjhfasvadsS.com"
password: "sdass123DadakjhdfsfadfSF45"
first_name: "shaun"
last_name: "dass"
age: "28"
phone_number: "123456789"
profile_picture: "www.jakljf.com"
loc: {
coordinates: [2]
0: 1
1: 2
-
type: "Point"
}-
_id: "55efc95e0e4556191cd36e5e"
__v: 0
}-
}-
}
位置字段会出现问题,因为如果我从模型中删除 loc 字段,POST 调用就可以正常工作
以下是我所做的命中/试验:
1) 将 userSchema.index({loc:'2d'});
改为 userSchema.index({loc:'2dsphere'});
2) 将 loc 模式更改为 Stackoverflow 中给出的所有内容。我想知道定义这个的正确方法。
3) 传递硬编码二维数组,但仍然显示 Location object expected, location array not in correct format"
这需要什么格式?
非常感谢您在这方面的任何帮助。谢谢。
最佳答案
MongoDB 2d
索引需要 legacy coordinates pairs格式,它只是一个坐标数组,如 [1, 2]
.
如果您需要 GeoJSON 支持,请使用 2dsphere index .
userSchema.index({loc:'2dsphere'});
关于arrays - 需要位置对象,位置数组的格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32472023/