MongoDb C# 驱动程序支持循环引用吗?
我正在考虑将一个小型副项目移植到使用 Mongo,因为在当前场景中使用 Nhibernate 越来越耗时。
我最初尝试了 NoRM,它有一个分支支持循环引用并且工作得很好,但是我找不到任何文档来表明官方 c# 驱动程序是否支持它。
这种情况以及为什么我有循环引用是因为我有一个位置对象,其中包含道路列表,每条道路都有到另一个位置的链接。它与探路器中的一组简单节点非常相似。
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Road> Roads { get; set; }
}
public class Road
{
public Location From { get; set; }
public Location To { get; set; }
}
现在的问题是我有一个由这些对象构建的整个世界(它们在真实场景中具有更多属性)并且它们都相互链接,但是如果无法处理循环引用,我不确定如何解决这个问题,因为每条路都需要知道起点和终点。
我知道一种折衷方案是删除位置对象,转而使用引用该位置的 Id,但随后我必须单独查询每个子位置。这仅执行一次,然后保存在内存中,因为有一张巨大的地图,其中包含所有可能的位置和所有可能的路线,因此可以在点之间找到快速路径。
可能是位置和道路不适合文档存储方法,可以通过其他方式存储......
I was looking at porting a small side project over to use Mongo, as it was getting more and more time consuming using Nhibernate for the current scenario.
I gave NoRM a try originally, and that had a branch which had support from cyclic references and worked fine, however I cannot find any documentation to indicate if the official c# driver supports it.
The situation and why I have a cyclic reference is because I have a location object, which contains a list of roads, each road has a link to another location. It is quite similar to a simple set of nodes in a pathfinder.
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Road> Roads { get; set; }
}
public class Road
{
public Location From { get; set; }
public Location To { get; set; }
}
Now the problem is I have a whole world built up out of these objects (they have more properties in the real scenario) and they all interlink, however without being able to handle cyclic references I am not sure how I can solve this problem, as each road needs to know a start and an end point.
I know one compromise is to just get rid of the location object, and instead have an Id that references the location, but then I have to query each sub location individually. This is only done once and then kept in memory as there is a huge map that contains all possible locations and all possible routes so quick paths can be found between points.
It may be a case of the Location and Roads are not suitable for a document storage approach and can be stored another way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
官方 C# 驱动程序根本不支持“引用”。字段的值可以是 ObjectID,但连接或引用的概念并未在官方 C# 驱动程序中真正实现。
当然,即使有“参考”支持,这些驱动程序仍然会执行多个查询。
鉴于您描述的情况,我建议查看图形数据库。有几种流行的,包括 Neo4J、Microsoft 的 Trinity,sones' GraphDB 和许多其他数据库。
The official C# driver doesn't really support "references" at all. The value of a field can be an ObjectID, but the concept of joins or references is not really implemented in the official C# driver.
Of course, even with "reference" support, these drivers would still be executing multiple queries.
Given the cases you've described, I would suggest looking at a graph database. There are several popular ones including Neo4J, Microsoft's Trinity, sones' GraphDB and a host of others.