C# MongoDB地理空间干草堆索引
如何使用MongoDB的10gen C#驱动程序创建一个 JS示例:C# MongoDB地理空间干草堆索引,c#,mongodb,geospatial,mongodb-.net-driver,C#,Mongodb,Geospatial,Mongodb .net Driver,如何使用MongoDB的10gen C#驱动程序创建一个 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 I
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”:“未指定其他字段”,“代码”:13317,“n”:0,“connectionId”:7,“ok”:1.0})
我让它与:
IMongoIndexKeys keys = new IndexKeysDocument {
{ "Position", "geoHaystack" },
{ "type", 1 }
};
IMongoIndexOptions options = new IndexOptionsDocument {
{ "bucketSize", 1 }
};
collection.EnsureIndex(keys, options);
我遇到的问题与集合中已加载的数据有关
然后,您应该能够查询:
var command = new CommandDocument {
{ "geoSearch", "foo" },
{ "near", new BsonArray { 33, 33 } },
{ "maxDistance", 6 },
{ "search", new BsonDocument { { "type", "restaurant" } } },
{ "limit", 30 }
};
database.RunCommand(command);
实际上,问题是索引方向被指定为字符串值
keys.Add("type", "1");
将其更改为整数
keys.Add("type", 1);
这将按预期工作