mongoDB索引创建
testDB库,testColl表索引:
PRIMARY> db.testColl.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "testDB.testColl"
},
{
"v" : 2,
"key" : {
"sendTime" : -1
},
"name" : "idx_sendTime",
"ns" : "testDB.testColl",
"background" : true
},
{
"v" : 2,
"key" : {
"fromUserId" : 1,
"sendTime" : -1,
"msgType" : 1
},
"name" : "idx_fromUserId_sendTime_msgType",
"ns" : "testDB.testColl",
"background" : true
},
{
"v" : 2,
"key" : {
"fromUserId" : 1,
"sendTime" : -1,
"groupType" : 1
},
"name" : "idx_fromUserId_sendTime_groupType",
"ns" : "testDB.testColl",
"background" : true
},
{
"v" : 2,
"key" : {
"groupId" : 1
},
"name" : "idx_groupId",
"ns" : "testDB.testColl",
"background" : true
}
]
转换为创建语句:
> use testDB
db.testColl.ensureIndex({"_id" : 1},{"background" : true}) // 默认就有,不需要手动创建
db.testColl.ensureIndex({"sendTime" : -1},{"background" : true})
db.testColl.ensureIndex({"fromUserId" : 1,"sendTime" : -1,"msgType" : 1},{"background" : true})
db.testColl.ensureIndex({"fromUserId" : 1,"sendTime" : -1,"groupType" : 1},{"background" : true})
db.testColl.ensureIndex({"groupId" : 1},{"background" : true})
参考
MongoDB 教程
https://www.jc2182.com/mongodb/mongodb-jiaocheng.html
网友评论