Reference Database in MongoDB
As shown in the Relationship chapter in MongoDB, to deploy a standardized database structure in MongoDB, we use the Referenced Relationship concept, also known as Manual References , in which we manipulate to store the id of the Documents referenced in another Document. However, in cases where a Document contains many references from different Collection, we use DBRefs in MongODB.
DBRefs and Manual References
In the following example, we will use DBRefs instead of Manual References. Suppose there is a database we use to store different types of addresses (home, office, mailing, .) in different collections (address_home, address_office, address_mailing, .). Now, when a user collection references an address, it also needs to determine which collection it will look at, based on the address type. In such a situation, when a Document references another Document from multiple Collection, we should use DBRefs.
Use DBRefs in MongoDB
There are 3 fields in DBRefs:
$ ref : This field identifies the Collection of the Document referenced.
$ id : This field identifies the _id field of the Document referenced.
$ db : This field is an arbitrary field, containing the name of the Database to which the Document is referenced.
Suppose a user document has an address field of DBRefs as follows:
{ "_id" : ObjectId ( "53402597d852426020000002" ), "address" : { "$ref" : "address_home" , "$id" : ObjectId ( "534009e4d852427820000002" ), "$db" : "tutorialspoint" }, "contact" : "987654321" , "dob" : "01-01-1991" , "name" : "Tom Benzamin" }
The address field of the DBRefs format here specifies that the address document is referenced in address_home collection under the tutorialspoint database and has an id of: 534009e4d852427820000002.
The following code will look at the Collection defined by the parameter $ ref (which is address_home in this case) for a Document with id defined by the parameter $ id in DBRefs.
> var user = db . users . findOne ({ "name" : "Tom Benzamin" }) > var dbRef = user . address > db [ dbRef . $ref ]. findOne ({ "_id" :( dbRef . $id )})
The above code returns the following address document , which is present in the address_home collection:
{ "_id" : ObjectId ( "534009e4d852427820000002" ), "building" : "22 A, Indiana Apt" , "pincode" : 123456 , "city" : "Los Angeles" , "state" : "California" }
According to Tutorialspoint
Previous post: Relationship in MongoDB
Next lesson: Covered Query in MongoDB
You should read it
May be interested
- Instructions on 2 ways to install MongoDB on Raspberry Piin this tutorial, tipsmake will guide you through the process of installing and setting up the mongodb server software on your raspberry pi.
- Replica Set in MongoDBreplication is the process of synchronizing data from multiple servers. replication provides redundancy and increased data availability for multiple data copies on many different database servers. replication protects a database from the loss of a particular server. replication also allows you to recover data from hardware errors or service disconnections. with additional data copies, you can use it for recovery, reporting, or backup.
- Index (Mong) in MongoDBindex (index) supports the resolution of queries more efficiently. without an index, mongodb must scan through all a collection's document to select the documents that connect to the query command. this scan may be ineffective and requires mongodb to handle a large amount of data.
- How to save React form data in Mongo Databasetry adding mongodb to your web stack to see how easy it is to store and query form data!
- Create Backup in MongoDBto create a database backup in mongodb, you should use the mongodump command. this command will dump all server data into dump directory. there are many options available from which you can limit the amount of data or backup created by remote server.
- Delete Collection in MongoDBthe db.collection.drop () method in mongodb is used to delete a collection from the database.
- How to Build a GraphQL API with Apollo Server and MongoDBgraphql provides a flexible alternative to the classic rest approach when you are building apis.
- Data type in MongoDBmongodb supports many different data types. the following are some typical data types.
- Query Document in MongoDBto query data from collection in mongodb, you need to use the find () method in mongodb.
- ObjectId in MongoDByou have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.