如果我有以下架构:
var zipSchema = new mongoose.Schema({
zc_code : String,
zc_population : Number,
zc_households : Number,
zc_housevalue : Number,
zc_householdincome : Number,
zc_statecode : String,
zc_state : String,
zc_city : String,
zc_cityname : String,
modified_at : Date,
center: {
type: {type: String},
coordinates: []
}
})
zipSchema.index({ center: '2dsphere' });
我试试这个:
var zipInfo = {
zc_code: '78746',
zc_population: 26928,
zc_households: 10839,
zc_housevalue: 344000,
zc_householdincome: 100571,
zc_latitude: '30.295657',
zc_long: '-97.813727',
zc_statecode: 'TX',
zc_state: 'Texas',
zc_city: 'AUSTIN',
center: {
coordinates: [-73.7567, 42.6525],
type: 'Point'
}
}
Zip.create(zipInfo, function(err) { if (err) console.log(err) })
我每次都会收到此错误:
MongoError:期望的位置对象,位置数组格式不正确
我错过了什么。我已经搜索了stackoverflow并看到了几个不同的geojson设置。我甚至尝试直接从mongoosejs测试中复制一些东西,但仍然会出错。我走到了尽头。任何帮助将不胜感激
答案 0 :(得分:1)
它对我来说也不起作用,但我刚刚通过命名几何字段来修复它:“loc”就像这里:http://docs.mongodb.org/manual/core/2dsphere/
这里是我使用的例子:
var cableSchema = new mongoose.Schema({
reference: String,
owner: String,
status: String,
setup: String,
model: Number,
loc: {
type: {type:String},
coordinates: Array
}
});
答案 1 :(得分:0)
我尝试了以下最新的猫鼬,它确实对我有用。你使用的是哪个版本?至于{type:{type:String}}问题,我认为这是因为type也是一个mongoose关键字类型:
var zipSchema = new mongoose.Schema({
zc_code : String,
zc_population : Number,
zc_households : Number,
zc_housevalue : Number,
zc_householdincome : Number,
zc_statecode : String,
zc_state : String,
zc_city : String,
zc_cityname : String,
modified_at : Date,
center: {
type: {type:String},
coordinates: [Number]
}
})
zipSchema.index({ center: '2dsphere' });
var zipInfo = {
zc_code: '78746',
zc_population: 26928,
zc_households: 10839,
zc_housevalue: 344000,
zc_householdincome: 100571,
zc_latitude: '30.295657',
zc_long: '-97.813727',
zc_statecode: 'TX',
zc_state: 'Texas',
zc_city: 'AUSTIN',
center: {
type: 'Point',
coordinates: [-73.7567, 42.6525]
}
}
var Zip = mongoose.model('Zip', zipSchema);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
Zip.create(zipInfo, function(err) {
if (err) console.log(err);
mongoose.disconnect();
})
});
结果是:
> db.zips.find().pretty()
{
"zc_code" : "78746",
"zc_population" : 26928,
"zc_households" : 10839,
"zc_housevalue" : 344000,
"zc_householdincome" : 100571,
"zc_statecode" : "TX",
"zc_state" : "Texas",
"zc_city" : "AUSTIN",
"_id" : ObjectId("522e2df92aacd22e89000001"),
"center" : {
"type" : "Point",
"coordinates" : [
-73.7567,
42.6525
]
},
"__v" : 0
}
> db.zips.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.zips",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"center" : "2dsphere"
},
"ns" : "test.zips",
"name" : "center_2dsphere",
"background" : true,
"safe" : null
}
]
>