Mongodb index on single field

Create an index on the studentId field of the students collection. 1 means index it in ascending and -1 means descending.
[code language=”javascript”]
db.students.createIndex({studentId: 1});
[/code]

With the above index, the following query will be using the studentId index for finding the results.
[code language=”javascript”]
db.students.find({studentId: 3});
db.students.find({studentId: {$gt: 30}});
[/code]

Show existing indexes in the students collection.
[code language=”javascript”]
db.students.getIndexes();
[/code]

Drop index
[code language=”javascript”]
db.students.dropIndex({studentId: 1});
[/code]

Search within Codexpedia

Custom Search

Search the entire web

Custom Search