位置对象预期,位置数组格式不正确

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

其余的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调用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 }- }- } 

位置字段给出了问题,因为POST调用工作得很好,如果我从模型中删除loc字段

下面是我做的命中/试用:1)更改userSchema.index({loc:'2d'});userSchema.index({loc:'2dsphere'}); 2)改变loc的架构,在Stackoverflow中给出的一切。 我想知道正确的方式来定义这个。 3)传递硬编码二维数组,但它仍然说Location object expected, location array not in correct format"这是什么格式需要?

任何在这方面的帮助,不胜感激。 谢谢。

MongoDB 2d索引需要传统坐标对格式,它只是一个像[1, 2]这样的坐标数组。

如果您需要GeoJSON支持,请使用2dsphere索引 。

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