3.32. MongoDB database reference

发布时间 : 2025-10-25 13:34:20 UTC      

Page Views: 9 views

In the previous section of the MongoDB relationship, we mentioned the reference to MongoDB to standardize the data structure document.

There are two types of MongoDB references:

  • Manual reference (Manual References)

  • DBRefs

3.32.1. DBRefs vs manual reference

Consider a scenario where we store different addresses (address, office address, email address, etc.) in different collections (address_home, address_office, address_mailing, etc.).

In this way, when we call different addresses, we also need to specify a collection, a document references a document from multiple collections, we should use DBRefs.

3.32.2. Use DBRefs

The form of DBRef:

{ $ref : , $id : , $db : } 

The meanings of the three fields are:

  • $ref: collection name

  • $id: referenced id

  • $db: database name, optional parameter

DBRef and field address are used in the user data document in the following example:

{ "_id":ObjectId("53402597d852426020000002"), "address": { "$ref": "address_home", "$id": ObjectId("534009e4d852427820000002"), "$db": "runoob"}, "contact": "987654321", "dob": "01-01-1991", "name": "Tom Benzamin" } 

address The DBRef field specifies that the referenced address document is the address_home collection under the runoob database, and id is 534009e4d852427820000002.

In the following code, we find the user address information for the specified id in the collection by specifying the $ref parameter (the address_home collection):

>var user = db.users.findOne({"name":"Tom Benzamin"}) >var dbRef = user.address >db[dbRef.$ref].findOne({"_id":(dbRef.$id)}) 

The above example returns the address data in the address_home collection:

{ "_id" : ObjectId("534009e4d852427820000002"), "building" : "22 A, Indiana Apt", "pincode" : 123456, "city" : "Los Angeles", "state" : "California" } 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。