美文网首页
MongoDB 集合

MongoDB 集合

作者: 薥劃 | 来源:发表于2020-07-23 10:37 被阅读0次

    创建集合

    语法格式:

    db.createCollection(name, options)

    参数说明:
    • name: 要创建的集合名称
    • options: 可选参数, 指定有关内存大小及索引的选项
    创建集合

    > use test
    switched to db test
    > db.createCollection("tre")
    { "ok" : 1 }

    如果要查看已有集合,可以使用 show collections 或 show tables 命令:

    > show collections
    tre

    创建固定集合 mycol,整个集合空间大小 6142800 KB, 文档最大个数为 10000 个。

    > db.createCollection("mycol", { capped : true, autoIndexId : true, size :
    ... 6142800, max : 10000 } )
    {
    "note" : "the autoIndexId option is deprecated and will be removed in a future release",
    "ok" : 1
    }
    > show collections
    mycol
    tre

    在 MongoDB 中,不需要创建集合。当你插入一些文档时,MongoDB 会自动创建集合。

    > db.mycol2.insert({"name" : "tre"})
    WriteResult({ "nInserted" : 1 })
    show collections
    mycol
    mycol2
    tre

    删除集合

    语法格式:

    db.collection.drop()

    删除集合 mycol2

    > db.mycol2.drop()
    true
    > show collections
    mycol
    tre

    从结果中可以看出 mycol2 集合已被删除。

    相关文章

      网友评论

          本文标题:MongoDB 集合

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