3.33. MongoDB overlay index query

发布时间 :2025-10-25 12:32:56 UTC      

The official MongoDB documentation states that the override query is the following query:

  • All query fields are part of the index

  • All query return fields are in the same index

Because all the fields that appear in the query are part of the index, MongoDB does not need to retrieve matching query criteria and return query results using the same index throughout the data document.

Because the index exists in RAM, it is much faster to get data from the index than to read it by scanning the document.

3.33.1. Use override index query

To test the override index query, use the following users collection:

{
   "_id": ObjectId("53402597d852426020000002"),
   "contact": "987654321",
   "dob": "01-01-1991",
   "gender": "M",
   "name": "Tom Benzamin",
   "user_name": "tombenzamin"
}

We create a federated index in the users collection with the fields gender and user_name:

>db.users.createIndex({gender:1,user_name:1})

注: Versions prior to 5.0 can use the db.collection.ensureIndex() , but ensureIndex() Has been removed since version 5.0, using the createIndex() Instead.

The index now overrides the following query:

>db.users.find({gender:"M"},{user_name:1,_id:0})

In other words, for the above query, MongoDB will not look in the database file. Instead, it extracts data from the index, which is a very fast data query.

Since the_ id field is not included in our index,_ id is returned by default in the query, so we can exclude it from the query result set of MongoDB.

If_ id is not excluded from the following example, the query will not be overwritten:

>db.users.find({gender:"M"},{user_name:1})

Finally, you cannot use an override index query if it is the following query:

  • All index fields are an array

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.