5

This is my first my first post in this forum. I am from RDBMS background and wanted to learn MongoDB which will help me in migrating our applications to MongoDB. I am creating two collections (customer & address).

Customer collection

Has generic information like name,gender,city,country,phone, address (which should be reference to another collection).

Address collection

Has an array of information like current address, office address, mailing address. I wanted to know, how can I add a reference to another collection? Considering this scenario, Customer Collection will be having an Address document which will have to contain reference to ids in Address Collection.

If someone could provide me a sample example for this model. It would be great. Thank you in advance.

6
  • ,What actually know about 'address' documents in both collection? Only reference id or else. Commented Feb 12, 2018 at 5:44
  • You don't need to have two separate collections. So, `{name:"john", gender: "male", city: "Chigago", country: "US", phone: [home: "555-1234", mobile: "555-4321"], address: [current: "streen 1", office: "lane 2", secred: "bat cave"]}
    – JJussi
    Commented Feb 12, 2018 at 5:57
  • @MdHaidarAliKhan, just wanted to reference the ids in customer collection. And the id's will be pointing to address collection. Commented Feb 12, 2018 at 6:24
  • @JJussi, thank you so much. But the main intention of writing this post is get an understanding of how do we reference the ids of a collection in another collection. So that I can use it for a different use case may be. Like a customer has several cars (each of different brands of different models). I wanted to maintain a separate collection named 'cars' with all the needed fields any only reference the ids of the car in customer collection. Commented Feb 12, 2018 at 6:27
  • @JJussi,you want to embedded document, The drawback is that if the embedded document keeps on growing too much in size, it can impact the read/write performance. Commented Feb 12, 2018 at 6:33

1 Answer 1

4

I wanted to know, how can I add a reference to another collection?

Relationships in MongoDB represent how various documents are logically related to each other. Relationships can be modeled via Embedded and Referenced approaches. Such relationships can be either 1:1, 1:N, N:1 or N:N.

It's possible through Modeling Referenced Relationships and DBRefs.

Here i am going to discuss about Modeling Referenced Relationships.

Let us consider the case of storing addresses for customers. So, one customer can have multiple addresses making this a 1:N relationship.

Following is the sample document structure of customer document

{
"_id":ObjectId("52ffc33cd85242f436000001"),
   "name": "Gokulnath Kumar",
   "gender": "Male",
   "city": "Doha",
   "country": "Qatar",
   "Phone": "777 95057",
   "address": "100A, Doha,Qatar"
})

Following is the sample document structure of address document −

{
"_id":ObjectId("52ffc4a5d85242602e000000"),
   "address": "100A, Doha,Qatar",
   "office address": " 200B, Doha Qatar",
   "mailing address": "[email protected]"
} 

Let's first i am going to write down here Modeling Embedded Relationships, for your better understanding.Even as @JJussi has already told that we can write down all documents information in one collection through modeling Embedded Relationship.

Note: The drawback is that if the embedded document keeps on growing too much in size, it can impact the read/write performance.

Modeling Embedded Relationships

Data in MongoDB has a flexible schema. Collections do not enforce document structure. Decisions that affect how you model data can affect application performance and database capacity.

This document describes a data model that uses embedded documents to describe relationships between connected data.

If your application frequently retrieves the address data with the name information, then your application needs to issue multiple queries to resolve the references. A more optimal schema would be to embed the address data entities in the customer data, as in the following document:

{
"_id":ObjectId("52ffc33cd85242f436000001"),
    "name": "Gokulnath Kumar",
   "gender": "Male",
   "city": "Doha",
   "country": "Qatar",
   "Phone": "777 95057",
   "address": [
{
"address": "100A, Doha,Qatar",
   "office address": " 200B, Doha Qatar",
   "mailing address": "[email protected]"
}]}

Then Modeling Referenced Relationships

This is the approach of designing normalized relationship. In this approach, both the customer and address documents will be maintained separately but the customer document will contain a field that will reference the address document's id field.

{
   "_id":ObjectId("52ffc33cd85242f436000001"),
   "name": "Gokulnath Kumar",
   "gender": "Male",
   "city": "Doha",
   "country": "Qatar",
   "Phone": "777 95057",
   "address_ids": [
      ObjectId("52ffc4a5d85242602e000000"),
      ObjectId("52ffc4a5d85242602e000001")
   ]
}

As shown above, the customer document contains the array field address_ids which contains ObjectIds of corresponding addresses. Using these ObjectIds, we can query the address documents and get address details from there. With this approach, we will need two queries: first to fetch the address_ids fields from customer document and second to fetch these addresses from address collection.

>var result = db.customer.findOne({"name":"Gokulnath Kumar"},{"address_ids":1})
>var addresses = db.address.find({"_id":{"$in":result["address_ids"]}})

For further your ref Here, Here and Here

2
  • Thank you so much for the detailed explanation. Could you also share the create script for this referenced relationship model? Commented Feb 13, 2018 at 12:19
  • In that last code, you are doing two queries, one to get all address ids and then to get all corresponding address objects. Can we combine them into a single query(maybe some kind of aggregation)? If so, do you know how one would do this? Commented Jan 22, 2020 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.