C# 中的 MongoDB 地理空间索引
我一直在尝试使用 C# 官方驱动程序创建和查询 MongoDB,但一次又一次地遇到同样的问题。问题是如何用地理信息创建数据。我只是找不到答案。
代码:
MongoUrl url = new MongoUrl("mongodb://xxx.xx.x.xx/mydb");
MongoServer server = MongoServer.Create(url);
MongoDatabase database = server.GetDatabase("mydb");
<-- 这工作正常
BsonDocument[] batch = {
new BsonDocument {
{ "name", "Bran" },
{ "loc", "10, 10" }
},
new BsonDocument {
{ "name", "Ayla" },
{ "loc", "0, 0" }
}
};
places.InsertBatch(batch);
<-- 该部分在某种程度上是错误的
places.EnsureIndex(IndexKeys.GeoSpatial("loca"));
var queryplaces = Query.WithinCircle("loca", 0, 0, 11);
var cursor = places.Find(queryplaces);
foreach (var hit in cursor)
{
foreach (var VARIABLE in hit)
{
Console.WriteLine(VARIABLE.Value);
}
}
<-- 我认为该部分应该显示两个文档,现在不显示任何文档。一个简单的发现就显示了它们两者。 会很高兴获得一些帮助。
I have been trying to get started but run into the same rock time after time trying to create and query MongoDB with C# official driver. The problem is how to create data with geo information. I am just not finding the answer.
Code:
MongoUrl url = new MongoUrl("mongodb://xxx.xx.x.xx/mydb");
MongoServer server = MongoServer.Create(url);
MongoDatabase database = server.GetDatabase("mydb");
<-- this works fine
BsonDocument[] batch = {
new BsonDocument {
{ "name", "Bran" },
{ "loc", "10, 10" }
},
new BsonDocument {
{ "name", "Ayla" },
{ "loc", "0, 0" }
}
};
places.InsertBatch(batch);
<-- that part is wrong somehow
places.EnsureIndex(IndexKeys.GeoSpatial("loca"));
var queryplaces = Query.WithinCircle("loca", 0, 0, 11);
var cursor = places.Find(queryplaces);
foreach (var hit in cursor)
{
foreach (var VARIABLE in hit)
{
Console.WriteLine(VARIABLE.Value);
}
}
<-- I think that part should show both documents, now showing none. A simple find shows them both.
Would be happy for some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的示例是 C# 语言的(重要的是要注意数组中的顺序,即经度、纬度 - 遵循 x、y 的更逻辑顺序,而不是纬度先于经度的更常用形式):
1.)首先,您的类需要 2.) 然后这里有
一些代码可以帮助您开始使用官方 C# 驱动程序并使用地理索引进行插入:
example below is in C# (it's important to note order in the array which is longitude, latitude- follows the more logical order of x,y as opposed to the more commonly used form where latitude precedes longitude) :
1.) first your class needs to have this:
2.) then here is some code to get you started with the official C# driver and doing an insert w/ use of geo indexing:
经过查看和搜索后,我在这里找到了答案: https://github .com/karlseguin/pots-importer/blob/master/PotsImporter/NodeImporter.cs
这将与我的第一段代码一起阅读,因为此修复 它。
需要澄清的是:问题中代码的问题在于位置信息不应存储为字符串,而应存储为 2 个元素 (x, y) 的数组。
After looking and searching I found the answer here: https://github.com/karlseguin/pots-importer/blob/master/PotsImporter/NodeImporter.cs
This to be read with my first piece of code as this fixes it.
As a point of clarification: The problem with the code in the question is that location information should not be stored as a string, but rather as an array of 2 elements (x, y).