Mongodb使用学习笔记(三)

Mongodb使用学习笔记(三),第1张

Mongodb使用学习笔记(三)

文章目录
    • 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({
    	,
    	,
    	{
    		upsert: ,
         	multi: ,
         	writeConcern: 
    	}
    })
    
  • 参数说明:
    • 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(
       ,
       {
         writeConcern: 
       }
    )
    
  • 注意,"_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" : "心情不好,想不通就好好想想吧" }
>

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5660444.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存