Reputation: 2434
If I have the following schema:
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' });
And the I try this:
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) })
I get this error every time:
MongoError: location object expected, location array not in correct format
What am I missing. I have searched stackoverflow and seen several different setups for the geojson stuff. I even tried directly copying some stuff from mongoosejs tests and still get errors. I am at a dead end. Any help would be appreciated
Upvotes: 0
Views: 1006
Reputation: 13
It didn't work for me either but I have just fixed it by naming the geometry field: "loc" like here: http://docs.mongodb.org/manual/core/2dsphere/
Here the example I used:
var cableSchema = new mongoose.Schema({
reference: String,
owner: String,
status: String,
setup: String,
model: Number,
loc: {
type: {type:String},
coordinates: Array
}
});
Upvotes: 1
Reputation: 3012
I tried the following with the latest mongoose and it did work for me. Which version are you using? And as for the { type: {type: String } } question, I believe it is because type is also a mongoose keyword type:
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();
})
});
The result was:
> 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
}
]
>
Upvotes: 0