Wednesday 28 August 2013

mongo shell indexes usage + miscellaneous comands

!!!!!!!!!!!!!!!!
Indexes
!!!!!!!!!!!!!!!!

db.students.ensureIndex({class:1, student_name:-1})
adding an index to a collection named students, having the index key be classstudent_name.
class: ascending sorted index
student_name: descending sorted index

db.system.indexes.find()
getting all added indexes in the database

db.students.getIndexes()
getting all added indexes in the collection students

db.students.dropIndex({'class':1, 'student_name':-1})
dropping index created by db.students.ensureIndex({class:1, student_name:-1}) command

db.students.ensureIndex({''student_name':-1} , {unique:true})
create index on student_name that is unique (this action, actually, force student_name be unique)

db.students.ensureIndex({''student_name':-1} , {unique:true} , {dropDups:true})
create index on student_name that is unique and delete all duplicates of the same student_name

db.students.ensureIndex({''student_name':-1} , {unique:true} , {sparce:true})
create index on student_name that is unique and only on the documents that has student_name not null

db.scores.totalIndexSize()
getting size of al indexes in the collection scores

db.scores.find({type:"essay" , score:50} ).hint({type: 1})
if in the collection score 2 indexes were added (one on type and one on score), the hint command forces to use index on type

db.scores.find({type:"essay" , score:50}).hint({$natural:1})
forces not tu use index at all

!!!!!!!!!!!!!!!!
Miscellaneous
!!!!!!!!!!!!!!!!

db.scores.find({type:"essay" , score:50}).explain()
getting information how find was actually perfomed

db.scores.stats()
getting collection statistics

db.system.profile.find()
finding logs of the selected DB

db.students.drop()
dropping students collection

db.dropDatabase()
dropping database

mongoimport -d school -c students < students.json
import documents from students.json to the school database to the students collection

No comments:

Post a Comment