MongoDB 地理空间 Haystack 索引

发布于 2024-12-07 09:22:09 字数 1331 浏览 1 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流绪微梦 2024-12-14 09:22:09

实际上问题是索引方向被指定为字符串值

 keys.Add("type", "1");

将其更改为整数

 keys.Add("type", 1);

这将按预期工作

Actually the problem was index direction was specified as a string value

 keys.Add("type", "1");

Change it to integer

 keys.Add("type", 1);

This will work as expected

吖咩 2024-12-14 09:22:09

我得到了它的工作:

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);

I got it working with:

IMongoIndexKeys keys = new IndexKeysDocument {
    { "Position", "geoHaystack" },
    { "type", 1 }
};

IMongoIndexOptions options = new IndexOptionsDocument {
    { "bucketSize", 1 }
};

collection.EnsureIndex(keys, options);

The problem I had was to do with data already loaded in the collection.

You should then be able to query with:

var command = new CommandDocument {
    { "geoSearch", "foo" },
    { "near", new BsonArray { 33, 33 } },
    { "maxDistance", 6 },
    { "search", new BsonDocument { { "type", "restaurant" } } },
    { "limit", 30 }
};
database.RunCommand(command);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文