美文网首页
【mongoDB】mongoDB组合索引创建

【mongoDB】mongoDB组合索引创建

作者: Bogon | 来源:发表于2022-12-15 15:24 被阅读0次

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

相关文章

网友评论

      本文标题:【mongoDB】mongoDB组合索引创建

      本文链接:https://www.haomeiwen.com/subject/thclqdtx.html