mongodb shell

  • show command
show dbs
use <db name>
show collections
db.<collectionName>.find()
  • list help for collection
db.<collectionName>.help()
  • create index
> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "perf_test.test"
        }
]
> db.test.createIndex({"test_name": 1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "perf_test.test"
        },
        {
                "v" : 1,
                "key" : {
                        "test_name" : 1
                },
                "name" : "test_name_1",
                "ns" : "perf_test.test"
        }
]
  • drop index
> db.test.dropIndex("test_name")
{
        "nIndexesWas" : 2,
        "ok" : 0,
        "errmsg" : "index not found with name [test_name]"
}
> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "perf_test.test"
        },
        {
                "v" : 1,
                "key" : {
                        "test_name" : 1
                },
                "name" : "test_name_1",
                "ns" : "perf_test.test"
        }
]
> db.test.dropIndex( {"test_name": 1} )
{ "nIndexesWas" : 2, "ok" : 1 }
> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "perf_test.test"
        }
]