需要位置对象,位置数组格式不正确

location object expected, location array not in correct format

我花了这么直接的事情。我只想使用 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;

用于post的其余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&latitude=2.0" 上进行 POST 调用时 我收到以下错误

{
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
}-
}-    
}

location 字段出现问题,因为如果我从模型中删除 loc 字段,POST 调用工作正常

下面是我做的hits/trials: 1) 将 userSchema.index({loc:'2d'}); 更改为 userSchema.index({loc:'2dsphere'}); 2) 将 loc 架构更改为 Whosebug 中给出的所有内容。不过,我想知道定义它的正确方法。 3) 传递硬编码 2d 数组,但它仍然说 Location object expected, location array not in correct format" 这个需要什么格式?

非常感谢这方面的任何帮助。谢谢。

MongoDB 2d 索引需要 legacy coordinates pairs 格式,它只是一个坐标数组,如 [1, 2].

如果您需要 GeoJSON 支持,请使用 2dsphere index

userSchema.index({loc:'2dsphere'});