Geospatial queries in MongoDB - Part 2 : {"err": "location object expected, location array not in correct format" "code": 16804}
In my last post we saw we had to create a geospatial index on the "pos" field as follows:
> db.foodtrucks.ensureIndex({pos:"2d"})
However, when executing the command above we get the following error:
> db.foodtrucks.ensureIndex({pos:"2d"})
}else{ // else add it
var newpair = {Longitude: doc.Longitude,Latitude: doc.Latitude} ;
newdoc.pos.push(newpair);
XDB.update({_id: doc._id},{$set:{pos: newpair}});
}
} while ( iter.hasNext() );
> db.foodtrucks.ensureIndex({pos:"2d"})
However, when executing the command above we get the following error:
> db.foodtrucks.ensureIndex({pos:"2d"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"ok" : 0,
"errmsg" : "location object expected, location array not in correct format",
"code" : 16804
}
After some further investigation I realized that we had some documents which didn't had any latitude/longitude information. Due to this some of the documents looked like this:
{
"_id":ObjectId("53d07bf1e3e11dacc21056a8"),
"locationid":437222,
"Applicant":"Natan's Catering",
"FacilityType":"Truck",
"cnn":188101,
"LocationDescription":"03RD ST: 18TH ST to 19TH ST (2101 - 2199) -- EAST --",
"Address":"2101 03RD ST",
"blocklot":4045004,
"block":4045,
"lot":4,
"permit":"13MFF-0102",
"Status":"EXPIRED",
"FoodItems":"Burgers: melts: hot dogs: burritos:sandwiches: fries: onion rings: drinks",
"X":"",
"Y":"",
"Latitude":"",
"Longitude":"",
"Schedule":"http://bsm.sfdpw.org/PermitsTracker/reports/report.aspx?title=schedule&report=rptSchedule¶ms=permit=13MFF-0102&ExportPDF=1&Filename=13MFF-0102_schedule.pdf",
"NOISent":"",
"Approved":"04/12/2013 02:43:48 PM",
"Received":"Apr 12 2013 2:33PM",
"PriorPermit":1,
"ExpirationDate":"03/15/2014 12:00:00 AM",
"Location":"",
"pos":{
"Longitude":"",
"Latitude":""
}
}
"_id":ObjectId("53d07bf1e3e11dacc21056a8"),
"locationid":437222,
"Applicant":"Natan's Catering",
"FacilityType":"Truck",
"cnn":188101,
"LocationDescription":"03RD ST: 18TH ST to 19TH ST (2101 - 2199) -- EAST --",
"Address":"2101 03RD ST",
"blocklot":4045004,
"block":4045,
"lot":4,
"permit":"13MFF-0102",
"Status":"EXPIRED",
"FoodItems":"Burgers: melts: hot dogs: burritos:sandwiches: fries: onion rings: drinks",
"X":"",
"Y":"",
"Latitude":"",
"Longitude":"",
"Schedule":"http://bsm.sfdpw.org/PermitsTracker/reports/report.aspx?title=schedule&report=rptSchedule¶ms=permit=13MFF-0102&ExportPDF=1&Filename=13MFF-0102_schedule.pdf",
"NOISent":"",
"Approved":"04/12/2013 02:43:48 PM",
"Received":"Apr 12 2013 2:33PM",
"PriorPermit":1,
"ExpirationDate":"03/15/2014 12:00:00 AM",
"Location":"",
"pos":{
"Longitude":"",
"Latitude":""
}
}
As we can see the "pos" entry above(in red) is empty. To address this issue, I made some changes to my JavaScript module to do null checks. Here is the updated code:
XDB = db.foodtrucks;
var iter = XDB.find()
do {
var doc = iter.next();
var newdoc = {};
newdoc.pos = [];
if(!doc.Longitude && !doc.Latitude){ // if the values are null, skip adding this entry
var iter = XDB.find()
do {
var doc = iter.next();
var newdoc = {};
newdoc.pos = [];
if(!doc.Longitude && !doc.Latitude){ // if the values are null, skip adding this entry
}else{ // else add it
var newpair = {Longitude: doc.Longitude,Latitude: doc.Latitude} ;
newdoc.pos.push(newpair);
XDB.update({_id: doc._id},{$set:{pos: newpair}});
}
} while ( iter.hasNext() );
Once we execute this, all the document in the collection will only have this entry if the values are not null. If we try to create a geospatial index now, it goes through successfully:
db.foodtrucks.ensureIndex({pos:"2d"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}
Comments
Post a Comment