mongodb索引操作
2020-06-15 本文已影响0人
simplerandom
查看索引
> db.myjihe.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.myjihe"
}
]
创建复合索引:"0"字段升序“p”降序
> db.myjihe.createIndex({"0":1,"p":-1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.myjihe.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.myjihe"
},
{
"v" : 2,
"key" : {
"0" : 1,
"p" : -1
},
"name" : "0_1_p_-1",
"ns" : "mydb.myjihe"
}
]
删除索引
> db.myjihe.dropIndex({"0":1,"p":-1})
{ "nIndexesWas" : 2, "ok" : 1 }
> db.myjihe.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.myjihe"
}
]