3.36. MongoDB Advanced Index

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

Consider the following collection of documents (users):

{
   "address": {
      "city": "Los Angeles",
      "state": "California",
      "pincode": "123"
   },
   "tags": [
      "music",
      "cricket",
      "blogs"
   ],
   "name": "Tom Benzamin"
}

The above document contains address Subdocuments and tags Array.

3.36.1. Index array field

Suppose we retrieve users based on tags, for which we need to index the array tags in the collection.

To create an index in an array, you need to index each field in the array in turn. So when we index the array tags, we create separate indexes for the values music, cricket, and blogs.

Create an array index using the following command:

>db.users.ensureIndex({"tags":1})

After creating the index, we can retrieve the tags field of the collection as follows:

>db.users.find({tags:"cricket"})

To verify that we used the index, we can use the explain command:

>db.users.find({tags:"cricket"}).explain()

If “cursor”: “BtreeCursor tags_1” is displayed in the execution result of the above command, the index has been used.

3.36.2. Index subdocument field

Suppose we need to retrieve the document through the city, state, and pincode fields, and since these fields are the fields of the subdocument, we need to index the subdocument.

Create an index for the three fields of the subdocument, with the following command:

>db.users.ensureIndex({"address.city":1,"address.state":1,"address.pincode":1})

Once the index is created, we can use the fields of the subdocument to retrieve the data:

>db.users.find({"address.city":"Los Angeles"})

The query expression does not necessarily follow the order of the specified index, and mongodb optimizes automatically. So the index created above will support the following query:

>db.users.find({"address.state":"California","address.city":"Los Angeles"})

The following queries are also supported:

>db.users.find({"address.city":"Los Angeles","address.state":"California","address.pincode":"123"})

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.