zoukankan      html  css  js  c++  java
  • mongoDB 创建数据库、删除数据库

    创建数据库

    use 命令

    MongoDB 用 use + 数据库名称 的方式来创建数据库。use 会创建一个新的数据库,如果该数据库存在,则返回这个数据库。

    语法格式

    use 语句的基本格式如下:

    use DATABASE_NAME

    范例

    创建一个名为 的数据库,使用 use 语句如下:

    >use mydb
    switched to db mydb
    

    使用命令 db 检查当前选定的数据库。

    >db
    mydb
    

    使用命令 show dbs 来检查数据库列表。

    >show dbs
    local     0.78125GB
    test      0.23012GB
    

    刚创建的数据库(mydb)没有出现在列表中。为了让数据库显示出来,至少应该插入一个文档。

    >db.mydb.insert({"name":"tutorials point"})
    >show dbs
    local      0.78125GB
    mydb       0.23012GB
    test       0.23012GB
    

    在 MongoDB 中,默认的数据库是 test,如果你没有创建任何数据库,那么集合就会保存在 test 数据库中。

    删除数据库

    dropDatabase() 方法

    MongoDB 的 dropDatabase() 命令用于删除已有数据库。

    语法格式

    dropDatabase() 命令的语法格式如下:

    db.dropDatabase()

    它将删除选定的数据库。如果没有选定要删除的数据库,则它会将默认的 test 数据库删除。

    范例

    首先使用 show dbs 来列出已有的数据库。

    >show dbs
    local      0.78125GB
    mydb       0.23012GB
    test       0.23012GB
    >

    如果想删除新数据库 <mydb>,如下面这样使用 dropDatabase() 方法:

    
    >use mydb
    switched to db mydb
    >db.dropDatabase()
    >{ "dropped" : "mydb", "ok" : 1 }
    >
    

    再来看一下数据库列表,确实删除了 <mydb>

    >show dbs
    local      0.78125GB
    test       0.23012GB
    >
     
  • 相关阅读:
    ACM ICPC 2008–2009 NEERC MSC A, B, C, G, L
    POJ 1088 滑雪 DP
    UVA 11584 最短回文串划分 DP
    POJ 2531 Network Saboteur DFS+剪枝
    UVa 10739 String to Palindrome 字符串dp
    UVa 11151 Longest Palindrome 字符串dp
    UVa 10154 Weights and Measures dp 降维
    UVa 10271 Chopsticks dp
    UVa 10617 Again Palindrome 字符串dp
    UVa 10651 Pebble Solitaire 状态压缩 dp
  • 原文地址:https://www.cnblogs.com/liang545621/p/7364335.html
Copyright © 2011-2022 走看看