MongoDB tutoria...login
MongoDB tutorial
author:php.cn  update time:2022-04-21 17:49:03

MongoDB relationships


MongoDB relationships represent the logical connections between multiple documents.

Relationships between documents can be established through embedding and citing.

Relationships in MongoDB can be:

  • 1:1 (1 to 1)

  • 1: N (1 To many)

  • N: 1 (Many to 1)

  • N: N (Many to many)


Next, let’s consider the relationship between users and user addresses.

A user can have multiple addresses, so it is a one-to-many relationship.

The following is the simple structure of the user document:

{
   "_id":ObjectId("52ffc33cd85242f436000001"),
   "name": "Tom Hanks",
   "contact": "987654321",
   "dob": "01-01-1991"
}

The following is the simple structure of the address document:

{
   "_id":ObjectId("52ffc4a5d85242602e000000"),
   "building": "22 A, Indiana Apt",
   "pincode": 123456,
   "city": "Los Angeles",
   "state": "California"
}

Embedded relationship

Using the embedded method, we can embed the user address into the user's document:

   "_id":ObjectId("52ffc33cd85242f436000001"),
   "contact": "987654321",
   "dob": "01-01-1991",
   "name": "Tom Benzamin",
   "address": [
      {
         "building": "22 A, Indiana Apt",
         "pincode": 123456,
         "city": "Los Angeles",
         "state": "California"
      },
      {
         "building": "170 A, Acropolis Apt",
         "pincode": 456789,
         "city": "Chicago",
         "state": "Illinois"
      }]
}

The above data is saved in a single document, which can be easily obtained and maintained. data. You can query the user's address like this:

>db.users.findOne({"name":"Tom Benzamin"},{"address":1})

Note: In the above query, db and users represent databases and collections.

The disadvantage of this data structure is that if users and user addresses continue to increase and the amount of data continues to increase, it will affect read and write performance.

Reference relationship

Reference relationship is a method often used when designing databases. This method separates the user data document and the user address data document by referencing the id of the document. fields to establish relationships.

{
   "_id":ObjectId("52ffc33cd85242f436000001"),
   "contact": "987654321",
   "dob": "01-01-1991",
   "name": "Tom Benzamin",
   "address_ids": [
      ObjectId("52ffc4a5d85242602e000000"),
      ObjectId("52ffc4a5d85242602e000001")
   ]
}

In the above example, the address_ids field of the user document contains the object id (ObjectId) array of the user's address.

We can read the object id (ObjectId) of these user addresses to obtain the user's detailed address information.

This method requires two queries. The first time is to query the object ID (ObjectId) of the user's address, and the second time is to obtain the user's detailed address information through the query ID.

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

php.cn