MongoDB文檔更新(一)

    MongoDB文檔更新:1.可以是採用刪除原文檔然後插入一個更新後的新文檔到數據庫中;2.基於原文檔使用修改器修改原文檔中的文檔屬性。

    下面主要說明的是使用基於原文檔使用update方法結合修改器修改文檔內容:
    修改的數據結構包括文檔中的簡單屬性、數組和嵌套文檔。
    1.$set修改器:用來指定文檔中某一個鍵的值,如果此鍵不存在的話就創建。
    如要修改如下文檔:
        > db.c1.find({"name":"user3"});
        { "_id" : ObjectId("4fc145e3703fa637a073651b"), "name" : "user3", "age" : 16 }

    將其中的"name"修改成"user16":
       > db.c1.update({"age":16},{"$set":{"name":"user16"}});
       > db.c1.find({"age":16});
        { "_id" : ObjectId("4fc145e3703fa637a073651b"), "age" : 16, "name" : "user16" }
    往用戶爲user16的文檔中添加一本書:
       > db.c1.update({"age":16},{"$set":{"book":"love story"}});
       > db.c1.find({"age":16});
{ "_id" : ObjectId("4fc145e3703fa637a073651b"), "age" : 16, "book" : "love story", "name" : "user16" }
       增加了“book”屬性,並添加了一本書。
    2.$unset修改器:與$set相對應,可以去掉某一個屬性值。
      如要刪上面文檔中的"book"屬性,可使用:
      db.c1.update({"age":16},{"$unset":{"book":1}});
      結果是:
      > db.c1.find({"age":16});
{ "_id" : ObjectId("4fc145e3703fa637a073651b"), "age" : 16, "name" : "user16" }其中的"book"屬性被刪除掉,"1"刪除掉book屬性。
    3.使用$set,$unset修改嵌套文檔:
    如下嵌套文檔:
    { "_id" : ObjectId("4fc145e3703fa637a073651b"),
      "address" : { "location" : "linkin street no5", "zip" : "108991" },
      "age" : 16,
      "name" : "user16" }
    修改其中的zip屬性爲"111111":
    > db.c1.update({"age":16},{"$set":{"address.zip":"111111"}});
    > db.c1.find({"age":16});
    { "_id" : ObjectId("4fc145e3703fa637a073651b"),
      "address" : { "location" : "linkin street no5", "zip" : "111111" },
      "age" : 16,
      "name" : "user16" }
    注意,在修改文檔的時候不要忘記使用"$set"修改器,否則的話原來的文檔會被
{"address.zip":"111111"}替代。
     4.數組修改器:對數組的修改,一般包括:添加元素值,刪除元素值。
     1).首先往一個文檔裏面添加一個數組,比如在這裏往:
     db.c1.find({"age":16});
    { "_id" : ObjectId("4fc145e3703fa637a073651b"),
      "address" : { "location" : "linkin street no5", "zip" : "111111" },
      "age" : 16,
      "name" : "user16" }
     添加一個"luckyNumber":[1,8,0]的鍵值對。可以使用前面的$set完成此操作。
     > db.c1.update({"age":16},{"$set":{"luckyNumber":[1,8,0]}});
     >db.c1.find({"age":16});
   { "_id" : ObjectId("4fc145e3703fa637a073651b"),
     "address" : { "location" : "linkin street no5", "zip" : "111111" },
     "age" : 16,
     "luckyNumber" : [ 1, 8, 0 ], "name" : "user16" }
     2).$push修改器:往"lucyNumber"中壓入一個數字,使用此修改器是往數組末尾追加一個數字。
     如:db.c1.update({"age":16},{"$push":{"luckyNumber":9}});
     > db.c1.find({"age":16});
{ "_id" : ObjectId("4fc145e3703fa637a073651b"), "address" : { "location" : "linkin street no5", "zip" : "111111" }, "age" : 16, "luckyNumber" : [ 1, 8, 0, 9 ], "name" : "user16" }
     其中luckyNumber多了一個數字9.
     如果繼續使用db.c1.update({"age":16},{"$push":{"luckyNumber":9}});會在再多出一個9.(此處省略)。
     3).$addToSet修改器:把數組當成一個類似於set集合,其中不能存放相同的值。
     如在2)的文檔中使用:db.c1.update({"age":16},{"$addToSet":{"luckyNumber":9}});
     結果會是:
      db.c1.find({"age":16});
{ "_id" : ObjectId("4fc145e3703fa637a073651b"), "address" : { "location" : "linkin street no5", "zip" : "111111" }, "age" : 16, "luckyNumber" : [ 1, 8, 0, 9 ], "name" : "user16" }
      其中luckyNumber還是隻有一個9.

     4).$pop數組數據彈出:{"$pop":{key:1}}彈出數組尾部數據,{"$pop":{key:-1}}彈出數組首部數據,
     如要彈出luckyNumber的末尾數"9":
     > db.c1.update({"age":16},{"$pop":{"luckyNumber":1}});
     > db.c1.find({"age":16});
   { "_id" : ObjectId("4fc145e3703fa637a073651b"),
     "address" : { "location" : "linkin street no5", "zip" : "111111" },
     "age" : 16,
     "luckyNumber" : [ 1, 8, 0 ],
     "name" : "user16" }
     彈出luckyNumber中的首位數1:     
     > db.c1.update({"age":16},{"$pop":{"luckyNumber":-1}});
     > db.c1.find({"age":16});
{ "_id" : ObjectId("4fc145e3703fa637a073651b"),
  "address" : { "location" : "linkin street no5", "zip" : "111111" },
  "age" : 16,
  "luckyNumber" : [ 8, 0 ],
  "name" : "user16" }
 其中的1被彈出。
      

      以上是關於文檔基本屬性和數組屬性的更新最基本的操作。
    





 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章