How do you create a Geospatial Haystack Index using the 10gen C# Driver for MongoDB
JS Example:
db.foo.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 })
C# Example that does not work:
BsonDocument keys = new BsonDocument();
keys.Add("pos", "geoHaystack");
keys.Add("type", "1");
IMongoIndexKeys indexKeys = new IndexKeysDocument(keys);
IndexOptionsDocument indexOptions = new IndexOptionsDocument("bucketSize", new BsonInt32(1));
collection.CreateIndex(indexKeys, indexOptions);
Gives this error:
MongoDB.Driver.MongoSafeModeException : Safemode detected an error ‘can only have 1 index plugin / bad index key pattern’. (Response was { “err” : “can only have 1 index plugin / bad index key pattern”, “code” : 13007, “n” : 0, “connectionId” : 6, “ok” : 1.0 }).
So if I remove the ‘type’ key, like this:
BsonDocument keys = new BsonDocument();
keys.Add("pos", "geoHaystack");
IMongoIndexKeys indexKeys = new IndexKeysDocument(keys);
IndexOptionsDocument indexOptions = new IndexOptionsDocument("bucketSize", new BsonInt32(1));
collection.CreateIndex(indexKeys, indexOptions);
I get this error:
MongoDB.Driver.MongoSafeModeException : Safemode detected an error ‘no other fields specified’. (Response was { “err” : “no other fields specified”, “code” : 13317, “n” : 0, “connectionId” : 7, “ok” : 1.0 }).
Actually the problem was index direction was specified as a string value
Change it to integer
This will work as expected