MongoDB - sample reference links

I read the reference materials from the MongoDB database documentation, but I really don’t understand the part of the “second query” for resolving the specified fields. ”Could you give me an example of this query so that I can better understand what they are talking about.

“Manual references relate to the practice of including one _id document field in another document. The application can then issue a second request to resolve the reference fields as necessary."

+6
source share
1 answer

The documentation is pretty clear in the section of the manual where you link to the Database Links section. The most important part of this understanding is contained in the opening statement on the page:

"MongoDB does not support joins. In MongoDB, some data is denormalized or stored with related data in documents to remove the need for connections. However, in some cases it makes sense to store related information in separate documents, usually in different collections or databases."

Additional information covers how you can choose to access data stored in another collection.

There is a DBRef specification which, without going into details, can > be implemented in some drivers as a way that when they are found in your documents, it will automatically extract (expand) the referenced document into the current document. This will be implemented behind the scenes with another request to this collection for a document of this _id.

In the case of the Link to the manual, this basically means that in your document there is only a field that has since it contains an ObjectId from another document. It only differs from DBRef in that it will never be processed using the base driver implementation, as it goes beyond the way you handle any subsequent extraction of this other soley document to you.

When:

> db.collection.findOne() { _id: <ObjectId>, name: "This", something: "Else", ref: <AnotherObjectId> } 

The ref field in the document is nothing more than a simple ObjectId and does nothing special. This allows you to send your own request to get information about the object, which relates to:

 > db.othercollection.findOne({ _id: <AnotherObjectId > }) { _id: <ObjectId> name: "That" something: "I am a sub-document to This!" } 

Keep in mind that all of these processes are performed on the client side through the driver API. In any case, none of these extracts of other documents occurs on the server.

+7
source

All Articles