mongoDB中是否可以使用循环对象? [英] Are circular object references possible in mongoDB?

查看:89
本文介绍了mongoDB中是否可以使用循环对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取一个mongoDB对象,例如Friend1包含另一个mongoDB对象Friend2,该对象又包含第一个对象Friend1,从而使它成为一个循环对象引用.

I'm trying to get a mongoDB object such as Friend1 contains another mongoDB object Friend2, which in turn contains the first object Friend1 essentially making this a circular object reference.

要么,要么类似的东西.

Either that, or something like that.

我希望能够更新Friend2.value并以某种方式从Friend1检索它,例如.Friend1.friend2.value

I want to be able to update Friend2.value and retrieve it from Friend1 somehow, like.. Friend1.friend2.value

我发现填充没什么用.它只是创建一个ObjectID引用,而不是如果我没看错的话,整个对象都被引用来检索...

I found populate to be not much useful.. it just creates an ObjectID reference, not the entire object is referenced to be retrieved, if I'm getting it right...

这可能吗?还是我应该使用的其他任何方式

Is this possible? Or anything else I should be using

推荐答案

嵌入式文档不是指向其他文档的指针,它们是数据的独立副本,因此尝试通过嵌入创建自引用对象将导致无限循环生成无限数据.

Embedded documents aren't pointers to other documents, they are independent copies of data, so trying to create a self-referencing object through embedding would result in an infinite loop producing infinite data.

以DBRef(数据库参考)形式存在的MongoDB文档之间的链接是软"的.它们实际上对数据库本身没有任何意义.数据库不会强制执行它们,不会检查它们的完整性,也不会为跟踪它们提供任何支持.

Links between MongoDB documents in form of DBRef's (Database References) are "soft". They don't actually have any meaning for the database itself. The database doesn't enforce them, doesn't check them for integrity and doesn't offer any support for following them.

因此您可以具有循环引用. MongoDB不在乎.但是您需要自己在应用程序层上解决它们.

So you can have circular references. MongoDB doesn't care. But you need to resolve them yourself on the application layer.

通常,对于大量使用对其他数据的引用的数据模型,MongoDB并不是一个很好的选择.当关系是数据的重要组成部分时,您可能需要看一下Neo4j之类的图形数据库.它们使遍历对象关系变得更加容易.

In general, MongoDB is not a good choice for data models which makes heavy use of references to other data. When relations are an important part of your data, you might want to take a look at graph databases like Neo4j. They make it much easier to traverse object relations.

这篇关于mongoDB中是否可以使用循环对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆