文章目录

  • Mongodb使用学习笔记(三)
    • 1. MongoDB 更新文档
      • 1.1 update()
      • 1.2 save()

Mongodb使用学习笔记(三)

1. MongoDB 更新文档

  • 参考:MongoDB 更新文档
  • 官方文档(要梯子)
  • MongoDB更新文档有update()和save()方法。update主要更新已存在的文档,save主要是存在"_id"的数据就更新,没有就新增。

1.1 update()

  • 参数结构:

    db.collection.update({<query>,<update>,{upsert: <boolean>,multi: <boolean>,writeConcern: <document>}
    })
    
  • 参数说明:
    • query : update的查询条件,类似sql update查询内where后面的。

    • update : update的对象和一些更新的操作符(如,,,inc…)等,也可以理解为sql update查询内set后面的

      • 更新的操作符有:

        • MongoDB 原子操作

        • 1.不包含操作:新的文档会直接将旧文档数据覆盖。

        • 2.$inc:给相应的字段加上参数所给的值。

        • 3.$set:给相应的字段设置参数所给的值。

          • 如果该字段没有在文档中,文档会增加该字段的数据
        • 4.$unset:删除指定的字段。

        • 5.$rename:重命名指定的字段。

        • 6.$push:对数组元素的操作符,只能对数组元素的字段使用,否则会报错,向数组中push单个值。push是增加数组元素的操作。

          • 在测试的时候,向已存在的非数组的元素进行push操作会报错。但是如果文档中未存在该字段,会新增该数组元素。
        • 7.$each:对数组元素的操作符,只能对数组元素的字段使用,否则会报错,向数组中push多个值。

          • $each要和$pash一起用
        • 8.$pop:对数组元素的操作符,只能对数组元素的字段使用,否则会报错,向数组中pop单个值。pop是删除数组元素的操作。只能删除第一个或者最后一个元素。-1表示删除第一个,1表示删除最后一个。

          • 不能对某个指定值进行删除
        • 9.$pullAll:对数组元素的操作符,只能对数组元素的字段使用,否则会报错,数组删除多个指定的值。

        • 10.$pull:删除数组中符合条件的值。和pullAll不一样的是,pullAll指定删除数组中存在的哪一个(些)值,在中括号里面一个个列出来,pull删除的是符合条件的值,写的是条件的表达式。

          • 条件可以用正则表达式,也可以用$in之类的
          • 上述操作记录:
          > use test
          switched to db test
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware6asdg", "serial_number" : "abcdef", "type" : "switch", "city" : "hn" }
          > db.hardware.update({name:/hardware/},{"name":"hardware1","serial_number":"000001","type":"switch","port":["01","02","03"],"count":1})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware1", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03" ], "count" : 1 }
          > db.hardware.update({name:/hardware/},{$inc:{"count":1}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware1", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03" ], "count" : 2 }
          > db.hardware.update({name:/hardware/},{$set:{"name":"hardware11"}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03" ], "count" : 2 }
          > db.hardware.update({name:/hardware/},{$set:{"fieldtest":"fieldtest"}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03" ], "count" : 2, "fieldtest" : "fieldtest" }
          > db.hardware.update({name:/hardware/},{$unset:{"fieldtest":"fieldtest"}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03" ], "count" : 2 }
          > db.hardware.update({name:/hardware/},{$rename:{"count":"countnumber"}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03" ], "countnumber" : 2 }
          > db.hardware.update({name:/hardware/},{$push:{"port":"04"}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03", "04" ], "countnumber" : 2 }
          > db.hardware.update({name:/hardware/},{$push:{"count":"04"}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03", "04" ], "countnumber" : 2, "count" : [ "04" ] }
          > db.hardware.update({name:/hardware/},{$push:{"countnumber":"04"}})
          WriteResult({"nMatched" : 0,"nUpserted" : 0,"nModified" : 0,"writeError" : {"code" : 2,"errmsg" : "The field 'countnumber' must be an array but is of type double in document {_id: ObjectId('61a8674e83e24c27ab4ce164')}"}
          })
          > db.hardware.update({name:/hardware/},{$each:{"port":["05","06"]}})
          WriteResult({"nMatched" : 0,"nUpserted" : 0,"nModified" : 0,"writeError" : {"code" : 9,"errmsg" : "Unknown modifier: $each. Expected a valid update modifier or pipeline-style update specified as an array"}
          })
          > db.hardware.update({name:/hardware/},{$push:{"port":{$each:["05","06"]}})
          ...
          ...
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03", "04" ], "countnumber" : 2, "count" : [ "04" ] }
          >  db.hardware.update({name:/hardware/},{$push:{"port":{$each:["05","06"]}})
          ... ^C> db.hardware.update({name:/hardware/},{$push:{"port":$each:["05","06"]})
          ... ^C> db.hardware.update({name:/hardware/},{$push:{"port":{$each:["05","06"]}}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03", "04", "05", "06" ], "countnumber" : 2, "count" : [ "04" ] }
          > db.hardware.update({name:/hardware/},{$pop:{"port":"06"}})
          WriteResult({"nMatched" : 0,"nUpserted" : 0,"nModified" : 0,"writeError" : {"code" : 9,"errmsg" : "Expected a number in: port: \"06\""}
          })
          > db.hardware.update({name:/hardware/},{$pop:{"port":1}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "01", "02", "03", "04", "05" ], "countnumber" : 2, "count" : [ "04" ] }
          > db.hardware.update({name:/hardware/},{$pop:{"port":-1}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03", "04", "05" ], "countnumber" : 2, "count" : [ "04" ] }
          > db.hardware.update({name:/hardware/},{$pullAll:{"port":["05","06"]}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03", "04" ], "countnumber" : 2, "count" : [ "04" ] }
          > db.hardware.update({name:/hardware/},{$pull:{"port":/6/}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03", "04" ], "countnumber" : 2, "count" : [ "04" ] }
          > db.hardware.update({name:/hardware/},{$pull:{"port":/4/}})
          WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
          > db.hardware.find()
          { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03" ], "countnumber" : 2, "count" : [ "04" ] }
          
    • upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。

      > use test
      switched to db test
      > db.hardware.find()
      { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03" ], "countnumber" : 2, "count" : [ "04" ] }
      > db.hardware.find({"name":"我是一个宕机的硬件"})
      > db.hardware.update({"name":"我是一个宕机的硬件"},{"name":"其实我是一个大帅逼,就是有点丑","city":"gz"},false,false)
      WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
      > db.hardware.find()
      { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03" ], "countnumber" : 2, "count" : [ "04" ] }
      > db.hardware.update({"name":"我是一个宕机的硬件"},{"name":"其实我是一个大帅逼,就是有点丑","city":"gz"},true,false)
      WriteResult({"nMatched" : 0,"nUpserted" : 1,"nModified" : 0,"_id" : ObjectId("61b062712eb247ef2a297b6f")
      })
      > db.hardware.find()
      { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03" ], "countnumber" : 2, "count" : [ "04" ] }
      { "_id" : ObjectId("61b062712eb247ef2a297b6f"), "name" : "其实我是一个大帅逼,就是有点丑", "city" : "gz" }
      >
      
    • multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

      > db.hardware.update({"city":"gz"},{$set:{"eat":"肠粉"}},false,false)
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      > db.hardware.find()
      { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03" ], "countnumber" : 2, "count" : [ "04" ] }
      { "_id" : ObjectId("61b062712eb247ef2a297b6f"), "name" : "其实我是一个大帅逼,就是有点丑", "city" : "gz", "eat" : "肠粉" }
      { "_id" : ObjectId("61b064f1eff9974c5a135de1"), "name" : "路人甲", "city" : "gz" }
      { "_id" : ObjectId("61b064fceff9974c5a135de2"), "name" : "路人乙", "city" : "gz" }
      { "_id" : ObjectId("61b06504eff9974c5a135de3"), "name" : "路人丙", "city" : "gz" }
      { "_id" : ObjectId("61b0650deff9974c5a135de4"), "name" : "路人丁", "city" : "gz" }
      > db.hardware.update({"city":"gz"},{$set:{"eat":"肠粉"}},false,true)
      WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 4 })
      > db.hardware.find()
      { "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03" ], "countnumber" : 2, "count" : [ "04" ] }
      { "_id" : ObjectId("61b062712eb247ef2a297b6f"), "name" : "其实我是一个大帅逼,就是有点丑", "city" : "gz", "eat" : "肠粉" }
      { "_id" : ObjectId("61b064f1eff9974c5a135de1"), "name" : "路人甲", "city" : "gz", "eat" : "肠粉" }
      { "_id" : ObjectId("61b064fceff9974c5a135de2"), "name" : "路人乙", "city" : "gz", "eat" : "肠粉" }
      { "_id" : ObjectId("61b06504eff9974c5a135de3"), "name" : "路人丙", "city" : "gz", "eat" : "肠粉" }
      { "_id" : ObjectId("61b0650deff9974c5a135de4"), "name" : "路人丁", "city" : "gz", "eat" : "肠粉" }
      >
      
    • writeConcern :可选,抛出异常的级别。

      • 菜鸟教程下面的评论有网友评论如下,可以看一看。

1.2 save()

  • 根据"_id"来更新或者新增文档数据

  • “_id"是文档主键。如果存在该”_id",新文档数据替换旧文档数据。如果没有该id,新增这个新文档数据。

  • 参数格式:

    • document : 文档数据。
    • writeConcern :可选,抛出异常的级别。
    db.collection.save(<document>,{writeConcern: <document>}
    )
    
  • 注意,"_id"要一模一样,包括ObjectId的函数

  • 操作记录:

> use test
switched to db test
> db.hardware.find()
{ "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03" ], "countnumber" : 2, "count" : [ "04" ] }
{ "_id" : ObjectId("61b062712eb247ef2a297b6f"), "name" : "其实我是一个大帅逼,就是有点丑", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b064f1eff9974c5a135de1"), "name" : "路人甲", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b064fceff9974c5a135de2"), "name" : "路人乙", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b06504eff9974c5a135de3"), "name" : "路人丙", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b0650deff9974c5a135de4"), "name" : "路人丁", "city" : "gz", "eat" : "肠粉" }
> db.hardware.save({"_id":"61a8674e83e24c27ab4ce164","word":"心情不好,想不通就好好想想吧"})
WriteResult({"nMatched" : 0,"nUpserted" : 1,"nModified" : 0,"_id" : "61a8674e83e24c27ab4ce164"
})
> db.hardware.find()
{ "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "name" : "hardware11", "serial_number" : "000001", "type" : "switch", "port" : [ "02", "03" ], "countnumber" : 2, "count" : [ "04" ] }
{ "_id" : ObjectId("61b062712eb247ef2a297b6f"), "name" : "其实我是一个大帅逼,就是有点丑", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b064f1eff9974c5a135de1"), "name" : "路人甲", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b064fceff9974c5a135de2"), "name" : "路人乙", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b06504eff9974c5a135de3"), "name" : "路人丙", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b0650deff9974c5a135de4"), "name" : "路人丁", "city" : "gz", "eat" : "肠粉" }
{ "_id" : "61a8674e83e24c27ab4ce164", "word" : "心情不好,想不通就好好想想吧" }
> db.hardware.save({"_id":ObjectId("61a8674e83e24c27ab4ce164"),"word":"我"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.hardware.find()
{ "_id" : ObjectId("61a8674e83e24c27ab4ce164"), "word" : "我" }
{ "_id" : ObjectId("61b062712eb247ef2a297b6f"), "name" : "其实我是一个大帅逼,就是有点丑", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b064f1eff9974c5a135de1"), "name" : "路人甲", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b064fceff9974c5a135de2"), "name" : "路人乙", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b06504eff9974c5a135de3"), "name" : "路人丙", "city" : "gz", "eat" : "肠粉" }
{ "_id" : ObjectId("61b0650deff9974c5a135de4"), "name" : "路人丁", "city" : "gz", "eat" : "肠粉" }
{ "_id" : "61a8674e83e24c27ab4ce164", "word" : "心情不好,想不通就好好想想吧" }
>

Mongodb使用学习笔记(三)相关推荐

  1. 尚硅谷+黑马程序员MongoDB视频学习笔记(一)

    本学习笔记是来源于学习B站上的尚硅谷和黑马的MongoDB教学视频而做的知识总结. 一.数据库(Database) 数据库是按照数据结构来组织.存在和管理数据的仓库.说白了,数据库就是存在数据的仓库. ...

  2. J2EE学习笔记三:EJB基础概念和知识 收藏

    J2EE学习笔记三:EJB基础概念和知识 收藏 EJB正是J2EE的旗舰技术,因此俺直接跳到这一章来了,前面的几章都是讲Servlet和JSP以及JDBC的,俺都懂一些.那么EJB和通常我们所说的Ja ...

  3. tensorflow学习笔记(三十二):conv2d_transpose (解卷积)

    tensorflow学习笔记(三十二):conv2d_transpose ("解卷积") deconv解卷积,实际是叫做conv_transpose, conv_transpose ...

  4. Ethernet/IP 学习笔记三

    Ethernet/IP 学习笔记三 原文为硕士论文: 工业以太网Ethernet/IP扫描器的研发 知网网址: http://kns.cnki.net/KCMS/detail/detail.aspx? ...

  5. iView学习笔记(三):表格搜索,过滤及隐藏列操作

    iView学习笔记(三):表格搜索,过滤及隐藏某列操作 1.后端准备工作 环境说明 python版本:3.6.6 Django版本:1.11.8 数据库:MariaDB 5.5.60 新建Django ...

  6. 吴恩达《机器学习》学习笔记三——多变量线性回归

    吴恩达<机器学习>学习笔记三--多变量线性回归 一. 多元线性回归问题介绍 1.一些定义 2.假设函数 二. 多元梯度下降法 1. 梯度下降法实用技巧:特征缩放 2. 梯度下降法的学习率 ...

  7. Python基础学习笔记三

    Python基础学习笔记三 print和import print可以用,分割变量来输出 import copy import copy as co from copy import deepcopy ...

  8. Mr.J-- jQuery学习笔记(三十二)--jQuery属性操作源码封装

    扫码看专栏 jQuery的优点 jquery是JavaScript库,能够极大地简化JavaScript编程,能够更方便的处理DOM操作和进行Ajax交互 1.轻量级 JQuery非常轻巧 2.强大的 ...

  9. MYSQL学习笔记三:日期和时间函数

    MYSQL学习笔记三:日期和时间函数 1. 获取当前日期的函数和获取当前时间的函数 /*获取当前日期的函数和获取当前时间的函数.将日期以'YYYY-MM-DD'或者'YYYYMMDD'格式返回 */ ...

最新文章

  1. Json数组列表中的数据分组排序、组内排序
  2. 单元测试unittest(基于数据驱动的框架:unittest+HTMLTestRunner/BeautifulReport+yaml+ddt)...
  3. oracle 删除用户 递归,ORACLE递归查询遍历详解
  4. [html] 表单可以跨域吗?
  5. mac mysql not found_mac版mysql安装后显示mysql: command not found咋整?
  6. ASP.NET通过OLE DB操作Excel
  7. 吉林大学超星MOOC学习通高级语言程序设计 C++ 实验03 模块化程序设计(2021级)
  8. linux 动态加载日志,Linux动态显示文件内容-linux tailf命令详解-Linux tailf命令退出-嗨客网...
  9. 软件工程——软件开发过程中用到的各种图
  10. 跟华为云一起,做未来城市的解谜人
  11. Xcode 8 size class
  12. web全栈工程师技能介绍
  13. 如何在PowerPoint中添加背景音乐
  14. 计算机开机显示器不亮,电脑开机显示器不亮该如何解决
  15. Detours库APIHook演示抓取微信界面绘制文字函数
  16. C语言中int、long等类型所占的字节数
  17. 高级项目管理-4、项目范围、进度、成本、质量管理
  18. 用于长延迟多径衰落环境下的强化UF-OFDM
  19. 编程获得CPU的主频
  20. 微型计算机输入输出设备ppt,微型计算机地输入输出设备.ppt

热门文章

  1. 2016奇虎360校园招聘编程题目
  2. 因虚拟光驱引发的困惑
  3. Android平台OpenGL ES图像处理(improving)
  4. 入门避雷需知:如何选择关于Python机器学习的书
  5. vue实现刷新页面随机切换背景图【适用于登陆界面】
  6. Unity制作随机数字抽奖小案例
  7. 中间件-ES-中文拼音多音字插件
  8. unity文本隐藏_AI论文中隐藏的笑话,全看懂的绝对是高手!
  9. 自己如何快速制作蓝色背景证件照
  10. html调色盘字符,油画基础知识:调色盘最详细讲解