美文网首页
MongoDB修改器

MongoDB修改器

作者: ChanZeeBm | 来源:发表于2018-01-03 22:56 被阅读0次

    $inc:增加或者减少已有的键值(如果不存在则新建一个)

    用法:

    db.collection.update({},{"$inc":{"key":1}})
    
    1. key:需要更新的键(不存在则新建一个),后面的值可以为正数或者负数,表示增加或者减少。
      PS: 不管该键值对存在与否,值只能是数值类型(e.g.浮点,整数)。
    例子:
    db.foo.insert({"count":1})
    db.foo.find()
    { "_id" : ObjectId("5a4ce27c50ac11b145adb170"), "count" : 1 }
    
    //use $inc
    db.foo.update({},{"$inc":{"count":-1}})
    db.foo.find()
    { "_id" : ObjectId("5a4ce27c50ac11b145adb170"), "count" : 0 }
    

    $set: 用来指定一个字段的值(不存在则新建)。

    用法:

    db.collection.update({},{"$set":{"key":"value"}})
    
    1. key: 就是key
    2. value:就是value
    例子:
    db.foo.insert({"Title":"What the hell"})
    db.foo.find()
    { "_id" : ObjectId("5a4ce50350ac11b145adb171"), "Title" : "What the hell" }
    
    // use $set
    db.foo.update({},{"$set":{"Content":"The hell is the code"}})
    db.foo.find()
    { "_id" : ObjectId("5a4ce50350ac11b145adb171"), "Title" : "What the hell", "Content" : "The hell is the code"
    

    $unset: 跟$set相反,删除某个字段。

    用法:

    db.collection.update({},{"$unset":{"key":"value"}})
    
    例子:
    //Depend on the exampe above
    db.foo.update({},{"$unset":{"Content":""}})
    db.foo.find()
    { "_id" : ObjectId("5a4ce50350ac11b145adb171"), "Title" : "What the hell" }
    

    $push: 如果数组已经存在,"$push"会向已有数组末尾加入一个元素,如果没有则创建一个。

    用法:

    db.collection.update({},{"$push":{"key":{"key":"value","key1":"value1" ...}}})
    
    例子:
    db.user.insert({"name":"Chan"})
    db.user.find()
    { "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan" }
    
    // use $push
    db.user.update({},{"$push":{"favourite":{"f_name":"eat","f_each_day":3}}})
    db.user.find()
    { "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan", "favourite" : [ { "f_name" : "eat", "f_each_day" : 3 } ] }
    

    子操作符$each:可以通过push一次性添加多个值:

    用法:

    db.collection.update({},{"$push":{"key":{"$each":["value1","value2","value3"]}}})
    
    例子:
    //Depend on the exampe above
    db.user.update({},{"$push":{"hair":{"$each":["black","ken","aye"]}}})
    db.user.find()
    { "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan", "favourite" : [ { "f_name" : "eat", "f_each_day" : 3 } ], "hair" : [ "black", "ken", "aye" ] }
    

    当然你也可以两个混合起来用:

    db.user.update({},{"$push":{"eye":{"$each":[{"l":"this is l","xyz":"this is xyz"},{"m":"this is m","abc":888.668}]}}})
    db.user.find()
    { "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan", "favourite" : [ { "f_name" : "eat", "f_each_day" : 3 } ], "hair" : [ "black", "ken", "aye" ], "eye" : [ { "l" : "this is l", "xyz" : "this is xyz" }, { "m" : "this is m", "abc" : 888.668 } ] }
    

    PS:如果"$each"指定的数组里面只有一个元素,则操作等同于普通"$push"操作。

    子操作符$slice

    用法:

    db.collection.update({},{"$push":{"key":{"$each":["value1,"value2","value3"],"$slice":10}}})
    

    以上会只保留数组里面的最前面10个元素,假如值是1-20,则只保留1-10.

    例子:
    db.user.update({},{"$push":{"face":{"$each":[1,2,3,4,5,6,7,8,9,10],"$slice":3}}})
     db.user.find()
    { "_id" : ObjectId("5a4ce7b950ac11b145adb172"), "name" : "Chan", "face" : [ 1, 2, 3 ] }
    

    如果$slice的值为负整数,则只保留后面几个值:

    db.user.update({},{"$push":{"skin":{"$each":["a","b","c","d","e","f","g","h"],"$slice":-3}}})
    db.user.find()
    { "_id" : ObjectId("5a4cec5b50ac11b145adb173"), "name" : "chan", "skin" : [ "f", "g", "h" ] }
    

    子操作符$sort:字面意思,按照指定的键排序:

    用法:

    db.collection.update({},{"$push":{"key":{"$each":[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}],"$sort":{"key2":-1}}}})
    

    PS:"$sort":{"key2":-1} 值为-1则是降序,1就是升序。(不允许为其他值)

    例子:
     db.user.update({},{"$push":{"skin":{"$each":[{"name":"chan","raiting":10},{"name":"zee","raiting":-5},{"name":"bm","raiting":30}],"$sort":{"raiting":1}}}})
    db.user.find()
    { "_id" : ObjectId("5a4cec5b50ac11b145adb173"), "name" : "chan", "skin" : [{ "name" : "zee", "raiting" : -5 }, { "name" : "chan", "raiting" : 10 }, { "name" : "bm", "raiting" : 30 } ] }
    

    "$slice","$sort"只能搭配"$each"使用,不能直接跟"$push"使用

    相关文章

      网友评论

          本文标题:MongoDB修改器

          本文链接:https://www.haomeiwen.com/subject/ptwknxtx.html