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. To test the override index query, use the following users collection: We create a federated index in the users collection with the fields gender and user_name: 注: Versions prior to 5.0 can use the The index now overrides the following query: 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: Finally, you cannot use an override index query if it is the following query: All index fields are an array 3.33.1. Use override index query ¶
{
"_id": ObjectId("53402597d852426020000002"),
"contact": "987654321",
"dob": "01-01-1991",
"gender": "M",
"name": "Tom Benzamin",
"user_name": "tombenzamin"
}
>db.users.createIndex({gender:1,user_name:1})
db.collection.ensureIndex()
, but
ensureIndex()
Has been removed since version 5.0, using the
createIndex()
Instead.>db.users.find({gender:"M"},{user_name:1,_id:0})
>db.users.find({gender:"M"},{user_name:1})