【mongoDB】mongoDB组合索引创建

2022-12-15  本文已影响0人  Bogon

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

上一篇 下一篇

猜你喜欢

热点阅读