MongoDB 地理空间 Haystack 索引
如何使用 MongoDB 的 10gen C# 驱动程序创建地理空间 Haystack 索引
JS 示例:
db.foo.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 })
不起作用的 C# 示例:
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);
给出此错误:
MongoDB.Driver.MongoSafeModeException:安全模式检测到错误“只能有 1 个索引插件/错误的索引键模式”。 (响应为{“err”:“只能有1个索引插件/错误的索引键模式”,“code”:13007,“n”:0,“connectionId”:6,“ok”:1.0})。
因此,如果我删除“type”键,如下所示:
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);
我收到此错误:
MongoDB.Driver.MongoSafeModeException:安全模式检测到错误“未指定其他字段”。 (响应为{“err”:“未指定其他字段”,“code”:13317,“n”:0,“connectionId”:7,“ok”:1.0})。
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 }).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上问题是索引方向被指定为字符串值
将其更改为整数
这将按预期工作
Actually the problem was index direction was specified as a string value
Change it to integer
This will work as expected
我得到了它的工作:
我遇到的问题是与集合中已加载的数据有关。
然后您应该能够使用以下命令进行查询:
I got it working with:
The problem I had was to do with data already loaded in the collection.
You should then be able to query with: