3.31. MongoDB relation

发布时间 :2025-10-25 12:33:00 UTC      

The relationship of MongoDB represents the logical interrelation between multiple documents.

Relationships can be established between documents through embedding and references.

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 the user and the user address.

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

The following is user The simple structure of the document:

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

The following is address The simple structure of the document:

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

3.31.1. Embedded relationship

Using the embedded method, we can embed the user’s 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. 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 Represents databases and collections.

The disadvantage of this data structure is that if the user and user address are increasing, the amount of data is getting larger and larger, which will affect the read and write performance.

Citation relation

Referential relationship is a method often used when designing a database, which separates the user data document from the user address data document by referencing the id Field to establish a relationship.

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

In the above example, the user document address_ids The field contains an array of objects id (ObjectId) of the user’s address.

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

This method requires two queries, the first query the object id (ObjectId) of the user address, and the second time the detailed address information of the user is obtained through the queried id.

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.