Saturday, 21 February 2015

mongoDB : Discovering Indexes


db.system.indexes.find()
Above statement gives you all the indexes present in given database.

> use school
switched to db school
>
> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "school.students" }
{ "v" : 1, "key" : { "student_id" : 1 }, "name" : "student_id_1", "ns" : "school.students" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "school.marks" }


db.students.getIndexes()
Above statement gives you all the indexes of students collection.


> db.students.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "school.students"
        },
        {
                "v" : 1,
                "key" : {
                        "student_id" : 1
                },
                "name" : "student_id_1",
                "ns" : "school.students"
        }
]


db.students.dropIndex({"student_id" : 1})
Above statement drop the index created on field “student_id”.

> db.students.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "school.students"
        },
        {
                "v" : 1,
                "key" : {
                        "student_id" : 1
                },
                "name" : "student_id_1",
                "ns" : "school.students"
        }
]
>
> db.students.dropIndex({"student_id" : 1})
{ "nIndexesWas" : 2, "ok" : 1 }
>
> db.students.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "school.students"
        }
]
>



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment