在mongo db 中增加、删除、修改文档有好多方法,这里简单记录一下我所知道的一些方法。

前置条件:

1、创建study数据库  use study;

2、创建persons集合,当第一次向persons集合中插入数据时,如果集合不存在会自动创建。当然也可以使用命令 db.createCollection("persons",{可以带一些参数,也可以不带})

3、db.persons.find({}) 可以查询persons集合中所有的文档。

一、增加文档

增加单个文档,使用 saveinsertOne insert 方法

db.persons.save({"userId":1});
db.persons.insertOne({"userId":2,"name":"huan"});
db.persons.insert({"userId":3,"name":"huan1993",address:["湖北","武汉"]});

注意:

              1、save 方法:如果传递了_id字段,如果_id字段的值在集合中已经存在,那么就会执行更新操作。

              2、insertOne方法:该方法不支持执行计划 (官网原文:insertOne() is not compatible with db.collection.explain().Use insert() instead.)。

              3、insert方法,当方法参数是 {} 时,那么是单条插入。

              4、insert和insertOne方法当手动指定 _id 字段的值,且值存在时,那么会报错。

批量增加文档,使用 insert insertMany 方法。

db.persons.insert([{"userId":4},{"userId":5,"address":["北京"]}]);
db.persons.insertMany([{"userId":6,"name":"1993"},{"userId":7,"name":"19930311"}]);

注意:

            1、当 insert方法中的参数是数组传递的时候就是批量插入

            2、insertMany 中的文档如果指定了 _id 字段,那么这个字段的值必须要唯一。

            3、insertMany 不支持执行计划

            4、默认情况下如果 insertMany 中的 _id 字段重复了,那么 到第一个 _id 重复前的记录都会插入成功,之后的都会失败,即使之后的记录数不存在 _id 重复,假如我要之后的记录也要执行,那么需要设置 ordered 为 false . 即: db.persons.insertMany([],{ordered:false})

二、修改文档

      ① 替换文档

      上方有个 userId 为 2文档,我想将将 name 的值更改成  name-update

    注意:

            1、如果前面的查询条件 {"userId":2} 查出了多条记录,只会更新第一条记录。

            2、次方法会将后面的文档替换满足条件的第一个文档。

    ② 修改文档中的某个字段

     注意: 1、此时也之后更新符合条件的第一条记录。

                 2、如果需要更新的字段在原文档中不存在则会增加此字段

    ③ 批量修改

        方式一:

        经过上面2步的操作,我们现在存在2条 {"name":"huan1993"} 的记录,现在要把2条记录的 userId 字段的值 同时修改成 10

    注意:

          1、在update方法中必须要将 第四个 参数 设置成 true

          2、批量更新只可用在 $XXX 的修饰器中,当删除上面的$set时,更新会报错。

    方式二:

        上方有2个userId=10的记录,但是它们的name的值不一样,下方就更新成一样的。

 

④ save 方法也可以进行update操作,当 _id 的值存在时,将进行更新操作。

看上方的图可以,存在2个userId=10的记录,现在我们要将第二条记录的 userId 的值更新成 12,使用 _id 这个字段的值去更新。
  

⑤  保存或更新操作

db.persons.update({"userId":13},{
$set: {
"age":25,
"name":"第一次执行,将会执行 插入操作,因此此时无法查询出来记录,第二次执行将会执行更新操作,因为此时记录已经存在了。"
}
},true,false);
db.persons.find({"userId":13});

    注意:

            1、此时一定要将 update 的第三个参数设置成 true, 否则当找不到记录时不会进行插入操作。第三个参数即为upsert

            2、当匹配到记录时将会执行更新操作。

三、文档的删除

① 删除全部文档

db.persons.remove({});

       注意:

           1、 db.persons.remove({}) 只是清空了集合persons中的数据,而 db.persons.drop() 将会删除 persons集合。

② 删除根据条件查询出来的文档

db.persons.remove({"userId":13});

四、完整脚本如下:

use study;

db.persons.save({"userId":1,"_id":ObjectId("5aa3b4d2ac88ec2a80e6483c")});
db.persons.insertOne({"userId":2,"name":"huan"});
db.persons.insert({"userId":3,"name":"huan1993",address:["湖北","武汉"]}); db.persons.find(); db.persons.insert([{"userId":4},{"userId":5,"address":["北京"]}]);
db.persons.insertMany([{"userId":6,"name":"1993"},{"userId":7,"name":"19930311"}]); db.persons.update({"userId":2 },{"name":"name-update"});
db.persons.find({"name":"name-update"}) db.persons.update({"name":"name-update"},{$set:{"userId":2,"name":"huan1993"}});
db.persons.find({"userId":2}); db.persons.find({"name":"huan1993"}); db.persons.update({"name":"huan1993"},{ $set: { "userId" : 10} },false,true);
db.persons.find({"name":"huan1993"}) db.persons.updateMany({"userId":10},{$set:{"name":"updateMany"}});
db.persons.find({"userId":10}); db.persons.save({"_id" : ObjectId("5aa3b4dbac88ec2a80e6483d"),"userId":12});
db.persons.find({"userId":12}); db.persons.update({"userId":13},{
$set: {
"age":25,
"name":"第一次执行,将会执行 插入操作,因此此时无法查询出来记录,第二次执行将会执行更新操作,因为此时记录已经存在了。"
}
},true,false);
db.persons.find({"userId":13}); db.persons.remove({});
db.persons.remove({"userId":13});
db.persons.drop();

最新文章

  1. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q105-Q108)
  2. [DEncrypt] MySecurity--安全加密/Base64/文件加密 (转载)
  3. java解析页面包jsoup
  4. c++学习笔记和思考
  5. MergeSort 归并排序
  6. 我的Windows日常——鼠标无法进行拖拽的解决方法
  7. H5新特性实现对class的增删改
  8. day39KNN算法和其他的算法
  9. CS229 6.18 CNN 的反向传导算法
  10. Linux下清除catalina.out文件
  11. iOS开发-UITextView实现PlaceHolder的方式
  12. Bioconductor应用领域之基因芯片
  13. Gridview中 LinkButton删除以及自带删除
  14. jzoj100031
  15. java之八大排序
  16. NuGet学习笔记(2) 使用图形化界面打包自己的类库[转]
  17. robotframework中文显示乱码
  18. Bootstrap总结二
  19. iOS开发 viewWillAppear:(BOOL)animated真机调试的时候不执行了怎么办
  20. APUE 学习笔记(三) 文件和目录

热门文章

  1. Mybatis-基本学习(上)
  2. JavaScrip中 Array.reduce()
  3. 求 10000 以内 n 的阶乘
  4. java线程day-01
  5. 概述 .NET 6 ThreadPool 实现
  6. 我们也有自带的缓存系统:PHP的APCu扩展
  7. webpack learn1-初始化项目1
  8. 替代jquery中的几个函数
  9. Spring Cloud Gateway 动态修改请求参数解决 # URL 编码错误传参问题
  10. ORACLE 坏块的模拟和查看