Mongodb compound index
Create a compound index on the studentId and class fields of the students collection. 1 means index it in ascending and -1 means descending.
[code language=”javascript”]
db.students.createIndex({"studentId": 1, "class": 1});
[/code]
With the above index, the following query will be using the studentId and class index for finding the results.
[code language=”javascript”]
db.students.find({"studentId": 3, "class": "math"});
[/code]
Show existing indexes in the students collection.
[code language=”javascript”]
db.students.getIndexes();
[/code]
Search within Codexpedia

Custom Search
Search the entire web

Custom Search
Related Posts