MongoDB Relationship Example

MongoDB has relationships and this shows how different documents are related to each other logically. These relationship can be designed through referenced and embedded ways. These relationship can be one to one , one to many, many to one or many to many. Here we will give examples of MongoDB relationship.

Embedded Relationships

In the embedded methodology, we will embed the address document inside the client document.

{
"_id":objectid("52ffc33cd85242f436000001"),
"contact": "9839810502",
"dob": "15-07-1995",
"name": "mAnsari",
"address": [
{
"building": "21, Raju's Apt",
"pincode": 694532,
"city": "Patna",
"state": "Bihar"
},

{
"building": "17, Ram's Apt",
"pincode": 657890,
"city": "Lucknow",
"state": "UP"
}]
}

This methodology keeps up all the related information in a single document which makes it simple to recover and keep up. The entire document could be recovered in a single query as mentioned below:

db.users.findone({"name":"mAnsari"},{"address":1})

In the referenced query listed above the  db and clients are the database and collection separately.

The downside is that if the inserted document continues developing excessively in size, it can affect the read/write execution.

Referenced Relationships

This is the methodology of outlining standardized relationship. In this methodology, both the client and location documents will be kept up independently however the client document will contain a field that will reference the location document’s id field.

{
"_id":objectid("52ffc33cd85242f436012345"),
"contact": "9839810502",
"dob": "15-07-1995",
"name": "mAnsari",
"address_ids": [
Objectid("52ffc4a5d85242602e12345"),
Objectid("52ffc4a5d85242602e12346")
]
}

As demonstrated over, the client document contains the array field address_ids which has Objectids of relating addresses . With the help these Objectids, we can query the location documents and get address details from that point. With this methodology, we will require two query: first to bring the address_ids fields from client document and second to get these address from address collection.

var result = db.users.findone({"name":"mAnsari"},{"address_ids":2})
var addresses = db.address.find({"_id":{"$in":result["address_ids"]}})

MongoDB Database References

As seen in the last part of MongoDB Relationships, to actualize a standardized database structure in MongoDB we utilize the idea of Referenced Relationships likewise alluded to as Manual References in which we physically store the referenced document’s id inside other document. Nonetheless, in situations where a document contains references from distinctive collections, we can utilize MongoDB DBRefs.

DBRefs vs Manual References

As a sample situation where we would utilize DBRefs rather than Manual References, consider a database where we are putting away diverse sorts of address (home, office, mailing, and so forth) in distinctive collection. Presently, when a client collection’s document references an address, it additionally needs to determine which collection to search according to the address . In such situations where a document references documents from numerous collection , we ought to utilize DBRefs.

Consider a specimen client document having Dbref field address as appeared:

{
"_id":objectid("65387543972fc5638726"),
"address": {
"$ref": "address_home",
"$id": Objectid("687458q29749fc897749"),
"$db": "j2eebrain"},
"contact": "9839810502",
"dob": "15-07-1995",
"name": "mAnsari"
}

The location Dbref field here tags that the referenced location document lies in address_home collection under j2eebrain database and has an id of 687458q29749fc897749.

The accompanying code progressively looks in the collection determined by $ref parameter (address_home for our situation) for a document with id as indicated by $id parameter in Dbref.

var client = db.users.findone({"name":"mAnsari"})
var dbref = user.address
db[dbref.$ref].findone({"_id":(dbref.$id)})

The above code gives back where its due location document show in address_home gathering:

{
"_id" : Objectid("534009e4d852427820000002"),
"building": "21, Raju's Apt",
"pincode": 694532,
"city": "Patna",
"state": "Bihar"
}


Make Database

MongoDB use Database name to make database. The command will make another database; in the event that it doesn’t exist else it will furnish a proportional payback database.

Command Structure:

 > Use Database name

Illustration:

On the off chance that you need to make a database with name <mydb>, then utilize DATABASE explanation would be as shown below:

use mydb
exchanged to db mydb

To check as of now chosen database utilize the command db

db
mydb

In the event that you need to check your databases list, then utilize the command show dbs.

show dbs
nearby  0.78125gb
test  0.23012gb

Your made database (mydb) is not introduce in rundown. To show database you have to embed atleast one document into it.

db.movie.insert({"name":" j2eebrain "})
show dbs
neighborhood  0.78125gb
mydb  0.23012gb
test  0.23012gb

In MongoDB default database is test. On the off chance that you didn’t make any database then collections will be put away in test database.