Last active
August 29, 2015 14:03
-
-
Save saintc0d3r/243c27ff85bb7dbcc86e to your computer and use it in GitHub Desktop.
Add indexes on a collection's columns in mongodb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let the collection's name is 'students' & we want to speed up the query's performance on the 'students' collection when we find documents in the collection by 'student_id'. | |
// In order to achieve this, we'll add an index for the 'student_id' column where we want to index it by ascending order. | |
db.students.ensureIndex({'student_id': 1}) | |
// Another case is when we want to speed up query which takes both 'student_id' & 'class' columns where the class would be descending order. | |
db.students.ensureIndex({'student_id':1, 'class':-1}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment