MongoDB 入门之基础 DDL

此文章主要记录部分主要的 MongoDB 的 DDL 操作。

  • db  查看当前所在的数据库(默认 test)
    > db
    test
    >
  • show dbs  查看当前数据库服务器上的数据库名字
    > show dbs
    admin  (empty)
    local  0.078GB
    mydb   0.078GB
    test   (empty)
    >
  • use dbSchema 切换到名为 dbSchema 的数据库上下文
    > use mydb
    switched to db mydb
    >

    当名为 dbSchema 不存在时,创建该数据库,使用 use 创建的数据库如果没有保存数据的话,在 switch 到其他数据库时,该空的数据库将别删除。

  • db.dropDatabase() 删除当期所在的数据库
    > db.dropDatabase()
    { "dropped" : "test2", "ok" : 1 }
    >
  • db.stats()  查看 database 的状态
    > db.stats()
    {
            "db" : "mydb",
            "collections" : 3,
            "objects" : 6,
            "avgObjSize" : 80,
            "dataSize" : 480,
            "storageSize" : 24576,
            "numExtents" : 3,
            "indexes" : 1,
            "indexSize" : 8176,
            "fileSize" : 67108864,
            "nsSizeMB" : 16,
            "dataFileVersion" : {
                    "major" : 4,
                    "minor" : 5
            },
            "extentFreeList" : {
                    "num" : 2,
                    "totalSize" : 262144
            },
            "ok" : 1
    }
    >
  • show collections  查看当前数据库内的集合
    > show collections
    collect
    system.indexes
    >
  • db.collection.stats()  查看集合的状态
    > db.collect.stats()
    {
            "ns" : "mydb.collect",
            "count" : 2,
            "size" : 224,
            "avgObjSize" : 112,
            "storageSize" : 8192,
            "numExtents" : 1,
            "nindexes" : 1,
            "lastExtentSize" : 8192,
            "paddingFactor" : 1,
            "systemFlags" : 0,
            "userFlags" : 1,
            "totalIndexSize" : 8176,
            "indexSizes" : {
                    "_id_" : 8176
            },
            "ok" : 1
    }
    >
  • db.createCollection(name,options)  创建集合
    db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean>, size: <number>, max: <number>} )
    > db.createCollection(
    ...     "test",
    ... {
    ...     capped: true,
    ...     autoIndexId: true,
    ...     size: 1024
    ... })
    { "ok" : 1 }
    >
    name: 集合的名字
    capped: 是否启用集合限制,如果开启需要制定一个限制条件,默认为不启用,如果你要开启 size,max 的限制,需要开启 capped
    size: 限制集合使用空间的大小,默认为没有限制
    max: 集合中最大条数限制,默认为没有限制
    autoIndexId: 是否使用_id作为索引,默认为使用(true或false)
    size的优先级比max要高
  • db.yourColl.drop() 删除集合
    > db.test.drop()
    true
    >
  • db.collection.ensureIndex(keys, options)  创建索引
    db.ensureIndex(keys, {background: <Boolean>, unique: <Boolean>, name: <String>, dropDups: <Boolean>, sparse: <Boolean>, expireAfterSeconds: <Integer>, v: <Index version>} )
    keys: 索引对{field:1},1表示升序,-1表示降序
    background: 是否在后台创建索引, false 在创建索引时将会阻断其他操作,但效率更高
    unique:是否唯一索引
    name:索引名
    dropDups:创建唯一索引,并删除重复值,散列索引中无效
    sparse:是否使用 sparse 索引
    expireAfterSeconds:TTL 索引的过期时间,过期后将自动删除。TTL 集合时是有限制的: (1)你不能创建 TTL 索引,如果要索引的字段已经在其他索引中使用。(2)索引不能包含多个字段。(3)索引的字段必须是一个日期的 bson 类型。
    v: 索引版本号
    > db.yourColl.ensureIndex({
        "username": 1
    },
    {
        unique: true
    })
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }
    >
  • db.collection.getIndexes()  查询索引
    > db.yourColl.getIndexes()
    [
            {
                    "v" : 1,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "mydb.yourColl"
            },
            {
                    "v" : 1,
                    "unique" : true,
                    "key" : {
                            "username" : 1
                    },
                    "name" : "username_1",
                    "ns" : "mydb.yourColl"
            }
    ]
    >

    如果需要查询系统中全部的索引,可以使用db.system.indexes.find()函数

  • db.collection.dropIndex(index)  删除某一索引
    index:可以是索引名,也可以是某一确定某一索引的文档,例如对于上文中的的索引可以同样可以通过 “username_1”,也可以通过“{username:1}”删除同一个索引。
    > db.yourColl.dropIndex({username:1})
    { "nIndexesWas" : 2, "ok" : 1 }
    >
  • db.collection.dropIndexes()  删除指定集合的全部索引
    > db.yourColl.dropIndexes()
    {
            "nIndexesWas" : 2,
            "msg" : "non-_id indexes dropped for collection",
            "ok" : 1
    }
  • db.collection.reIndex()  删除某集合上的所有索引然后重建
    > db.yourColll.reIndex()
    {
            "nIndexesWas" : 2,
            "nIndexes" : 2,
            "indexes" : [
                    {
                            "key" : {
                                    "_id" : 1
                            },
                            "name" : "_id_",
                            "ns" : "mydb.yourColl"
                    },
                    {
                            "unique" : true,
                            "key" : {
                                    "username" : 1
                            },
                            "name" : "username_1",
                            "ns" : "mydb.yourColl"
                    }
            ],
            "ok" : 1
    }
    >
原文地址:https://www.cnblogs.com/tannerBG/p/4023712.html