Embedding and referencing mongodb documents

Source: Internet
Author: User
Tags findone
Mongodb is between relational and non-relational data. mongodb's join query can be implemented through reference. You can embed the document content into another document or reference the document content to another document. Embedding means embedding a certain type of data, such as an array containing more data, into the document itself. Reference means creating a reference

Mongodb is between relational and non-relational data. mongodb's join query can be implemented through reference. You can embed the document content into another document or reference the document content to another document. Embedding means embedding a certain type of data, such as an array containing more data, into the document itself. Reference means creating a reference

Mongodb is between relational and non-relational data. mongodb's join query can be implemented through reference. You can embed the document content into another document or reference the document content to another document. Embedding means embedding a certain type of data, such as an array containing more data, into the document itself. Reference means creating a reference that contains the data of another document. It is equivalent to a relational database. I. Embedding for example: I want to use a relational database to record CD, DVD, and purchase information. In this data, one table is required to collect the CD, and the other table is used to store the CD song information. Therefore, to query specific information, you may need to query multiple tables. For mongodb or other non-relational databases, it is easier to embed such information. This method simplifies the database and ensures that all relevant information is stored in a single document, while retrieving data faster. The data structure is as follows: relational
|_media|_cds|_id, artist, title, genre, releasedate|_ cd_tracklists|_cd_id, songtitle, length
Non-relational
|_media|_items|_
For non-relational databases, the document looks as follows:
[        {                "_id" : ObjectId("5353463193efef02c962da73"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind",                "Tracklist" : [                        {                                "Track" : "1",                                "Title" : "Smells like teen spirit",                                "Length" : "5:02"                        },                        {                                "Track" : "2",                                "Title" : "In Bloom",                                "Length" : "4:15"                        }                ]        }]
In the above example, the song information is embedded in the document itself. In a relational database, at least two tables are required. In a non-relational database, only one set and one document are required. When retrieving information, you only need to load data from one document to the memory, instead of loading multiple documents. 2. In many scenarios, embedding data in documents is sufficient for many applications, as shown above. However, you sometimes need to reference another document. Document references in mongodb are solved by executing additional queries. Mongodb provides two methods for implementation: manual reference and use of the DBRef standard. 2.1 manual reference is the simplest and most direct method. When you manually reference data, store the _ id value of other documents in your documents. Example:
> use ttlsa_comswitched to db ttlsa_com> apress = ( { "_id" : "Apress", "Type" : "Technical Publisher", "Category" : ["IT", "Software","Programming"] } ){        "_id" : "Apress",        "Type" : "Technical Publisher",        "Category" : [                "IT",                "Software",                "Programming"        ]}> db.publisherscollection.insert(apress)
Reference
> book = ( { "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress","Author" : ["Membrey,Peter","Plugge, Eelco","Hawkins, Tim"] } ){        "Type" : "Book",        "Title" : "Definitive Guide to MongoDB, the",        "ISBN" : "987-1-4302-3051-9",        "Publisher" : "Apress",        "Author" : [                "Membrey,Peter",                "Plugge, Eelco",                "Hawkins, Tim"        ]}> db.mediaCollection.insert(book)
Search
> book = db.mediaCollection.findOne({"ISBN" : "987-1-4302-3051-9"}){        "Author" : [                "Hawkins, Tim",                "Plugge, Eelco"        ],        "ISBN" : "987-1-4302-3051-9",        "Publisher" : "Apress",        "Title" : " Different Title 2",        "Type" : "Book",        "_id" : ObjectId("5353462f93efef02c962da71")}> book.PublisherApress> db.publisherscollection.findOne( { _id : book.Publisher } ){        "_id" : "Apress",        "Type" : "Technical Publisher",        "Category" : [                "IT",                "Software",                "Programming"        ]}
2.2? DBRefDBRef provides a more formal standard reference between documents. In DBRef, database references are embedded in Object Storage with standard JSON/BSON. Syntax :? {$ Ref: , $ Id: [, $ Db: ]} The name of the referenced set. The _ id value of the referenced document. $ Db is optional. Name of the database where the referenced document is located.
> apress = ( { "Type" : "Technical Publisher", "Category" :["IT","Software","Programming"] } ){        "Type" : "Technical Publisher",        "Category" : [                "IT",                "Software",                "Programming"        ]}> db.publisherscollection.save(apress)> apress{        "Type" : "Technical Publisher",        "Category" : [                "IT",                "Software",                "Programming"        ],        "_id" : ObjectId("53588c221697e7511678752c")}> book = { "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Author": ["Membrey, Peter","Plugge,Eelco","Hawkins, Tim"], Publisher : [ new DBRef ('publisherscollection',apress._id) ] }{        "Type" : "Book",        "Title" : "Definitive Guide to MongoDB, the",        "ISBN" : "987-1-4302-3051-9",        "Author" : [                "Membrey, Peter",                "Plugge,Eelco",                "Hawkins, Tim"        ],        "Publisher" : [                DBRef("publisherscollection", ObjectId("53588c221697e7511678752c"))        ]}> db.media.save(book)> db.media.find().toArray()[        {                "_id" : ObjectId("53588ce01697e7511678752d"),                "Type" : "Book",                "Title" : "Definitive Guide to MongoDB, the",                "ISBN" : "987-1-4302-3051-9",                "Author" : [                        "Membrey, Peter",                        "Plugge,Eelco",                        "Hawkins, Tim"                ],                "Publisher" : [                        DBRef("publisherscollection", ObjectId("53588c221697e7511678752c"))                ]        }]
III. relatively embedded: The data of small sub-documents does not change frequently. When the final consistency is acceptable, document growth is small and it is often necessary to perform secondary queries to obtain data reading and Fast Reference: large sub-documents non-volatile data when real-time consistency is necessary for large document growth, it is often necessary to exclude data from the results for fast writing

Original article address: Embed and reference the mongodb document. Thank you for sharing the original article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.