mongodb update

methods

    db.collection.findOneAndReplace().
    db.collection.findOneAndUpdate().
    db.collection.findAndModify().
    db.collection.save().
    db.collection.bulkWrite().

语句结构

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)
  • query : 查询条件,类似sql update查询内where后面的。
  • update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
  • upsert : 可选,如果不存在update的记录,是否插入objNew,true为插入,默认是false。
  • multi : 可选, 默认是false,只更新找到的第一条记录。为true,就把按条件查出来多条记录全部更新。
  • writeConcern :可选,抛出异常的级别

Replace the Document

To replace the entire content of a document except for the _id field, pass an entirely new document as the second argument to db.collection.replaceOne() or db.collection.update(). When replacing a document, the replacement document must consist of only <field> : <value>.

if you do include the _id field, it must have the same value as the current value

db.collection.replaceOne

replace the first document that matches the filter name equals "abc" with the new document:

db.users.replaceOne(
   { name: "abc" },
   { name: "amy", age: 34, type: 2, status: "P", favorites: { "artist": "Dali", food: "donuts" } }
)

Operators

field

$inc

用法:{ $inc : { field : value } }
意思对一个数字字段field增加value

$mul 乘

$rename 重命名

$set 赋值

$unset 移除field

$setOnInsert

找不到满足条件的文档且insert选项设置为true时才起作用,否则被忽略

db.products.update(
  { _id: 1 },
  {
     $set: { item: "apple" },
     $setOnInsert: { defaultQty: 100 }
  },
  { upsert: true }
)

$min

Only updates the field if the specified value is less than the existing field value.

$max

Array

$占位符

更新找到的那个元素的值

db.students.update(
   { _id: 1, grades: 80 },
   { $set: { "grades.$" : 82 } }
)

db.students.update(
   { _id: 4, "grades.grade": 85 },
   { $set: { "grades.$.std" : 6 } }
)

$addToSet

Adds elements to an array only if they do not already exist in the set.

{ _id: 1, letters: ["a", "b"] }

The following operation appends the array [ "c", "d" ] to the letters field:

db.test.update(
   { _id: 1 },
   { $addToSet: {letters: [ "c", "d" ] } }
)

The letters array now includes the [ "c", "d" ] array as an element:

{ _id: 1, letters: [ "a", "b", [ "c", "d" ] ] }

使用each
一次插入多个值

db.inventory.update(
   { _id: 2 },
   { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } }
 )
//变成
{
  _id: 2,
  item: "cable",
  tags: [ "electronics", "supplies", "camera", "accessories" ]
}

$pop

移除数组第一个或最后一个元素
-1 to remove the first element of an array and 1 to remove the last element

{ $pop: { <field>: <-1 | 1>, ... } }

$pullAll

删除在value列表中的所有元素

{ _id: 1, scores: [ 0, 2, 5, 5, 1, 0 ] }

db.survey.update( { _id: 1 }, { $pullAll: { scores: [ 0, 5 ] } } )

{ "_id" : 1, "scores" : [ 2, 1 ] }

$pull

从现有的数组中移除与指定条件相匹配的所有值

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

$push

添加一个元素,不过不要忘了$each

db.students.update(
   { name: "joe" },
   { $push: { scores: { $each: [ 90, 92, 85 ] } } }
)

Modifiers

$each

$slice

截取数组,正的就从前边开始

{ "_id" : 1, "scores" : [ 40, 50, 60 ] }

db.students.update(
   { _id: 1 },
   {
     $push: {
       scores: {
         $each: [ 80, 78, 86 ],
         $slice: -5
       }
     }
   }
)

{ "_id" : 1, "scores" : [  50,  60,  80,  78,  86 ] }

//只截取、不增加
db.students.update(
  { _id: 3 },
  {
    $push: {
      scores: {
         $each: [ ],
         $slice: -3
      }
    }
  }
)

$sort

重新排列数组顺序, 1 for ascending or -1 for descending

db.students.update(
   { _id: 1 },
   {
     $push: {
       quizzes: {
         $each: [ { id: 3, score: 8 }, { id: 4, score: 7 }, { id: 5, score: 6 } ],
         $sort: { score: 1 }
       }
     }
   }
)

Important

The sort document refers directly to the field in the documents and does not reference the containing array field quizzes; i.e. { score: 1 } and not { "quizzes.score": 1}

$position

决定在数组的哪个位置插入数据
比如带有$each的例子

{
  $push: {
    <field>: {
       $each: [ <value1>, <value2>, ... ],
       $position: <num> 
    }
  }
原文地址:https://www.cnblogs.com/jcuan/p/5699110.html