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.
I want to be able to update Friend2.value
and retrieve it from Friend1
somehow, like.. Friend1.friend2.value
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.
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.
So you can have circular references. MongoDB doesn't care. But you need to resolve them yourself on the application layer.
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.