zoukankan      html  css  js  c++  java
  • mongodb update 字符 操作(补充)

    数组相关更新操作

     3. $push

    用来增加数组。若没有改列,则会自动增加。

    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt" }

    > db.tianyc03.update({'name':'xtt'},{$push:{companies:'neu'}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu" ], "name" : "xtt" }

    > db.tianyc03.update({'name':'xtt'},{$push:{companies:'alibaba'}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba" ], "name" : "xtt" }

    4. $addToSet

    #push操作不会检查数组中元素是否重复,如果需要将不重复的数据加入数组,需要使用$addToSet

    > db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'alibaba'}})

    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba" ], "name" : "xtt" }
    > db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'congxing'}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
    > db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'congxing'}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
    >

    5. $pop

    通过$pop可以从数组中移除数据。此时数据被看成是一个队列。使用{$pop:{key:1}}将从队列末尾移除元素,使用{$pop:{key:-1}}将从队列开头移除元素。

    > db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:['c1','c2']}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing", [ "c1", "c2" ] ], "name" : "xtt" }

    > db.tianyc03.update({'name':'xtt'},{$pop:{companies:1}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
    >
    > db.tianyc03.update({'name':'xtt'},{$pop:{companies:-1}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
    >

    6. $pull

    除了 $pop,也可以使用 $pull 来删除数组中的元素,此时可以根据元素值进行匹配,将匹配上的元素全部删除。

    > db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'teletek'}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "congxing", "teletek" ], "name" : "xtt" }
    > db.tianyc03.update({},{$pull:{companies:'congxing'}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "teletek" ], "name" : "xtt" }

    > db.tianyc03.update({},{$pull:{companies:'alibaba'}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
    >

    批量更新

    3. 批量更新

    默认情况下,update只更新匹配到的第一行记录。将update的第四个参数设置为true,则批量更新搜索到的所有记录。

    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "count" : 11 }
    { "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
    { "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
    { "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 244 }
    { "_id" : ObjectId("510f1c8f8474cd8af024e2d1"), "count" : 24 }
    #将所有count值增加10,但却只增加了第一行。

    > db.tianyc03.update({},{$inc:{count:10}}) 
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "count" : 21 }
    { "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
    { "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
    { "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 244 }
    { "_id" : ObjectId("510f1c8f8474cd8af024e2d1"), "count" : 24 }

    #再增加一次,还是只增加了第一行。

    > db.tianyc03.update({},{$inc:{count:10}})
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "count" : 31 }
    { "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 26 }
    { "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 23 }
    { "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 244 }
    { "_id" : ObjectId("510f1c8f8474cd8af024e2d1"), "count" : 24 }

    #使用批量更新,达到目的。

    > db.tianyc03.update({},{$inc:{count:10}},false,true)
    > db.tianyc03.find()
    { "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "count" : 41 }
    { "_id" : ObjectId("510f1a9f6010b3da16ea6618"), "count" : 36 }
    { "_id" : ObjectId("510f1aa76010b3da16ea6619"), "count" : 33 }
    { "_id" : ObjectId("510f1b718474cd8af024e2d0"), "count" : 254 }
    { "_id" : ObjectId("510f1c8f8474cd8af024e2d1"), "count" : 34 }

  • 相关阅读:
    HDU 4745 Two Rabbits (区间DP)
    HDU 1007 Quoit Design最近点对( 分治法)
    acdream 小晴天老师系列——我有一个数列! (ST算法)
    HDU 3592 World Exhibition (差分约束,spfa,水)
    HDU 5501 The Highest Mark (贪心+DP,经典)
    HDU 5500 Reorder the Books (水题)
    HYSBZ 1010 玩具装箱toy (决策单调DP)
    POJ 3017 Cut the Sequence (单调队列优化DP)
    Vijos P1243 生产产品 (单调队列优化DP)
    PIVOT&UNPIVOT
  • 原文地址:https://www.cnblogs.com/cynleely/p/7843898.html
Copyright © 2011-2022 走看看