学习mongo系列(三) update() save()

一、update()方法
  >db.user.update({"name":"user1"},{$set:{"title":"admin"}})

    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  > db.user.find()
    { "_id" : ObjectId("56946fba3a18f4867aecbcd1"), "name" : "user1", "password" : "123456", "title" : "admin" }
    { "_id" : ObjectId("5694714a3a18f4867aecbcd2"), "name" : "user2", "passsword" :"123456" }
  > db.user.update({"name":"user1"},{$set:{"name":"admin"}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  > db.user.find()
    { "_id" : ObjectId("56946fba3a18f4867aecbcd1"), "name" : "admin", "password" : "123456", "title" : "admin" }
    { "_id" : ObjectId("5694714a3a18f4867aecbcd2"), "name" : "user2", "passsword" :"123456" }
  >注意你的括号,结构,以及字段的名称

  *以上语句只会修改第一条发现的文档,如果你要修改多条相同的文档,则需要设置 multi 参数为 true。

>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})

二、save()方法

  > db.user.save({"_id":ObjectId("5694714a3a18f4867aecbcd2"),"name":"aaaaaaaaa","password":"123456"})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  > db.user.find().pretty()
    {
      "_id" : ObjectId("56946fba3a18f4867aecbcd1"),
      "name" : "admin",
      "password" : "123456",
      "title" : "admin"
    }
    {
      "_id" : ObjectId("5694714a3a18f4867aecbcd2"),
      "name" : "aaaaaaaaa",
      "password" : "123456"
      }
  >

三、

----------------------2016-5-26 15:15:58--

上一篇:/boot/grub/menu.lst详解


下一篇:求链表中倒数第k个节点