sha*_*aun 7 arrays mongoose mongodb node.js restify
我花了这么直接的事情.我只想使用nodejs,mongoose,restify stack对用户模型进行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;
Run Code Online (Sandbox Code Playgroud)
其余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();
},
Run Code Online (Sandbox Code Playgroud)
现在,当我进行POST调用时,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&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
}-
}-
}
Run Code Online (Sandbox Code Playgroud)
如果我从模型中删除loc字段,POST调用工作正常,位置字段会出现问题
以下是我所做的点击/试验:1)更改userSchema.index({loc:'2d'});为userSchema.index({loc:'2dsphere'});
2)将loc模式更改为Stackoverflow中给出的所有内容.我想知道正确的方法来定义它.3)传递硬编码2d阵列,但仍然说它Location object expected, location array not in correct format"需要什么格式?
非常感谢在这方面的任何帮助.谢谢.
Fel*_*tus 10
MongoDB 2d索引需要传统的坐标对格式,它只是一个坐标数组[1, 2].
如果您需要GeoJSON支持,请使用2dsphere索引.
userSchema.index({loc:'2dsphere'});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4025 次 |
| 最近记录: |