nickf
nickf

Reputation: 546143

Error creating Geospatial index in MongoDB

I have a collection ('stops') with 14000 records imported from a CSV. Included in each record is some geospatial data, which I've converted into an array, so each essentially looks like this:

{
  _id: ...,
  // other fields
  "loc": [
    153.019073,
    -27.467834
  ]
}

When I run db.stops.ensureIndex({ loc: '2d' }) it gives the error:

location object expected, location array not in correct format

I guess that something's wrong in one of the fields, but I can't figure out which one. Any ideas?

Upvotes: 0

Views: 593

Answers (1)

nickf
nickf

Reputation: 546143

The problem was that there was a single record which wasn't imported correctly (probably an empty line at the end of the file).

To find it (and later remove it), I used the $where operator:

db.stops.find({ $where: 'typeof this.loc[0] != "number"' })

Upvotes: 1

Related Questions