zoukankan      html  css  js  c++  java
  • MongoDB:删除操作

     

    一. 根据查询条件删除文档

    1. 查询 id=1 的所有文档
    test:PRIMARY> db.test_1.find({id:1})
    { "_id" : ObjectId("58a11caca14d421caf8c45db"), "id" : 1, "name" : "name_1" }
    { "_id" : ObjectId("58a11cada14d421caf8c45dc"), "id" : 1, "name" : "swrd" }
    
    1. 删除 id=1 的所有文档,查看里面已无id等于1的数据
    test:PRIMARY> db.test_1.remove({id:1})
    WriteResult({ "nRemoved" : 2 })
    test:PRIMARY> db.test_1.find({id:1})
    test:PRIMARY>
    

    二. 删除一个集合中的所有文档

    1. 查询 test_1 集合的所有文档
    test:PRIMARY> db.test_1.find()
    { "_id" : ObjectId("58a11dd8a14d421caf8c45dd"), "id" : 2, "name" : "name_2" }
    { "_id" : ObjectId("58a11dd8a14d421caf8c45de"), "id" : 3, "name" : "name_3" }
    { "_id" : ObjectId("58a11dd8a14d421caf8c45df"), "id" : 4, "name" : "name_4" }
    { "_id" : ObjectId("58a11dd8a14d421caf8c45e0"), "id" : 5, "name" : "name_5" }
    { "_id" : ObjectId("58a11dd8a14d421caf8c45e1"), "id" : 6, "name" : "name_6" }
    { "_id" : ObjectId("58a11dd8a14d421caf8c45e2"), "id" : 7, "name" : "name_7" }
    
    1. 删除 test_1 集合的所有文档
    test:PRIMARY> db.test_1.remove({})
    WriteResult({ "nRemoved" : 6 })
    test:PRIMARY> db.test_1.find()
    

    三、删除集合

    test:PRIMARY> show tables;
    test_1
    test_2
    test_3
    test_4
    test:PRIMARY> db.test_4.drop()
    true
    test:PRIMARY> show tables;
    test_1
    test_2
    test_3
    

    四、删除数据库

    test:PRIMARY> show dbs
    local          0.000GB
    log            0.089GB
    student        0.000GB
    swrd           0.000GB
    test           0.000GB
    test:PRIMARY> db
    test
    test:PRIMARY> db.test.getDB()
    test
    test:PRIMARY> show tables;
    test_1
    test:PRIMARY> db.dropDatabase()
    { "dropped" : "test", "ok" : 1 }
    test:PRIMARY> show dbs
    local          0.000GB
    log            0.089GB
    student        0.000GB
    swrd           0.000GB
    

    在执行删除整个数据库前,要谨慎,执行db命令查看当前的使用的数据库,可确保误删除,造成数据的丢失.

     
     
     
  • 相关阅读:
    转:因果图法
    转:测试用例设计方法--正交试验法详解
    转:Navicat Premium15破解教程
    转:Excel2016怎么样快速比较两列数据是否相同
    zabbix使用问题
    zabbix系列之七——安装后配置二Userparameters
    zabbix系列之八——安装后配置三Triggers
    zabbix系列之六——安装后配置二Items
    zabbix系列之五——安装后配置一
    zabbix系列之四——快速使用
  • 原文地址:https://www.cnblogs.com/xiaoyuxixi/p/13803127.html
Copyright © 2011-2022 走看看