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

查看:25
本文介绍了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

我发现 populate 没有多大用处.它只是创建了一个 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天全站免登陆