MongoDb C# 驱动程序支持循环引用吗?

发布于 2024-12-14 15:38:05 字数 738 浏览 1 评论 0原文

我正在考虑将一个小型副项目移植到使用 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 技术交流群。

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

发布评论

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

评论(1

彻夜缠绵 2024-12-21 15:38:07

官方 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.

It may be a case of the Location and Roads are not suitable for a document storage approach and can be stored another way...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文