1

I have imported this data from an excel document.

 {
    "_id" : ObjectId("57440d63a458f726a8f95bfe"),
    "state" : "WEST BENGAL",
    "bank" : "STATE BANK OF INDIA",
    "ifsc" : "SBIN0000001",
    "micr code" : "700002021",
    "branch" : "KOLKATA MAIN",
    "address" : "SAMRIDDHI BHAWAN, 1 STRAND ROAD, KOLKATA 700 001",
    "latlong" : "22.569435,88.342932",
    "latitude" : "22.569435",
    "longitude" : "88.342932",
    "city" : "KOLKATA",
    "district" : "KOLKATA",
    "loc" : [
            "88.34",
            "22.57"
        ]
}

This is my sample document. I am unable to create a geospatial index on loc. How to create?

Mrig
  • 11,612
  • 2
  • 13
  • 27
Coded9
  • 159
  • 2
  • 14
  • how are you trying to create index? – Atish May 24 '16 at 10:26
  • db.sbi.ensureIndex({loc:"2d"}) – Coded9 May 24 '16 at 10:27
  • looks like the loc field is not in valid format. The values of the array may be either arrays, as in [ 55.5, 42.3 ], or embedded documents, as in { lng : 55.5 , lat : 42.3 }. – Atish May 24 '16 at 10:33
  • I tried converting them to float values,but it didn't help db.sbi.find().forEach(function(doc) { doc.loc = [parseFloat(doc.longitude).toFixed(2),parseFloat(doc.latitude).toFixed(2)]; db.sbi.save(doc); }); – Coded9 May 24 '16 at 10:34
  • can you show us the error you get while creating the index? – Atish May 24 '16 at 10:40
  • ```db.sbi.ensureIndex({"loc":"2d"}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "ok" : 0, "errmsg" : "location object expected, location array not in correct format", "code" : 16804 }``` – Coded9 May 24 '16 at 10:44
  • try this: { "_id" : ObjectId("57440d63a458f726a8f95bfe"), "state" : "WEST BENGAL", "bank" : "STATE BANK OF INDIA", "ifsc" : "SBIN0000001", "micr code" : "700002021", "branch" : "KOLKATA MAIN", "address" : "SAMRIDDHI BHAWAN, 1 STRAND ROAD, KOLKATA 700 001", "latlong" : "22.569435,88.342932", "latitude" : "22.569435", "longitude" : "88.342932", "city" : "KOLKATA", "district" : "KOLKATA", "loc" : [ ["88.34", "22.57" ]] } – Atish May 24 '16 at 10:45
  • "errmsg" : "geo values must be 'legacy coordinate pairs' for 2d indexes got the error message – Coded9 May 24 '16 at 10:48
  • the challenge here is to convert coordinates to float values in loc.. – Coded9 May 24 '16 at 10:51

1 Answers1

0

Values of arrays need to be in special format to create 2D Index.

Geospatial Indexes

The values of the array may be either arrays, as in [ 55.5, 42.3 ], or embedded documents, as in { lng : 55.5 , lat : 42.3 }.

I could create 2d index on following document. I think you would need to revisit excel import process.

{
    "_id" : ObjectId("57440d63a458f726a8f95bfe"),
    "state" : "WEST BENGAL",
    "bank" : "STATE BANK OF INDIA",
    "ifsc" : "SBIN0000001",
    "micr code" : "700002021",
    "branch" : "KOLKATA MAIN",
    "address" : "SAMRIDDHI BHAWAN, 1 STRAND ROAD, KOLKATA 700 001",
    "latlong" : "22.569435,88.342932",
    "latitude" : "22.569435",
    "longitude" : "88.342932",
    "city" : "KOLKATA",
    "district" : "KOLKATA",
    "loc" : [
            88.34,
            22.57
        ]
}
Sandesh
  • 1,036
  • 5
  • 13
  • How to convert this part to float values ? "loc" : [ "88.34", "22.57" ] ```db.sbi.find().forEach(function(doc) { doc.loc = [parseFloat(doc.longitude).toFixed(2),parseFloat(doc.latitude).toFixed(2)]; db.sbi.save(doc); });``` I tried this – Coded9 May 24 '16 at 10:53
  • ```db.sbi.find().forEach(function(doc) { db.sbi.update( { _id: doc._id}, { $set : { "loc" : [parseFloat(doc.longitude),parseFloat(doc.latitude)] } } ) })``` Done! Thanks – Coded9 May 24 '16 at 11:10
  • Great. Thanks for sharing it. – Sandesh May 24 '16 at 13:42