4

I am attempting to use MongoDB's Geospatial Indexing by querying for latitude and longitude points around a certain point using MongoDB's find method. I keep getting the error:

MongoError: can't find any special indices: 2d (needs index), 2dsphere (needs index)

I'm not sure where the documentation is for this after Googling for about an hour. Also I can't find any good explanations. Here is the Schema that I created using Mongoose:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var EventSchema = new Schema ({
  name: String,
  description: String,
  location: {type: [String], index: '2dsphere'},
  time: Date,
  photo: Buffer,
  activity: String
});

mongoose.model('Event', EventSchema);

I then use the find method to find other Event documents around the point that the user provides.

var maxDistance = 0.09;
var lonLat = {$geometry: {type: 'Point', coordinates: [34.09,124.34] }};

Event.find({
  'geo': {
    $near: lonLat,
    $maxDistance: maxDistance
  }
}).exec(function(err, events) {
  if(err) {
    throw err;
  } else {
    return events;
  }
});

What syntax do I have wrong here? Am I missing something huge? Any urls to any documentation will be great as well besides this link.

Community
  • 1
  • 1
Matt Goo
  • 1,118
  • 1
  • 11
  • 12
  • did you actually make the indexes? http://docs.mongodb.org/manual/core/geospatial-indexes/ quic search for mongodb 2d indexes – Sammaye Oct 06 '13 at 21:44

1 Answers1

2

Did you try to create the index with this sentence?

db.event.ensureIndex({ "location" : "2d" } )

It's a little confusing your example. I don't get if 'location' are your coordinates or 'geo', because you create the schema with the former and you query with the latter.

In both cases, a good idea is to execute

db.event.findOne().pretty()

So you can check the format of your collection.

Also with this command you can check your current indexes.

db.event.getIndexes()

All of this works if you are working on 'event' collection.

mazzi
  • 86
  • 1
  • 5