数据库程序员mongodb基础操作

mongodb基础操作之update更新操作

2021-01-12  本文已影响0人  蚁族的乐土

mongodb update api操作分为update、updateOne、updateMany、replaceOne四种

1. update

 

说明:

修改现有文档或集合中的文档。该方法可以修改一个或多个现有文档的特定字段,或者完全替换现有文档,具体取决于更新参数。

默认情况下,update()方法更新单个文档。设置Multi参数以更新匹配查询条件的所有文档。

语法:

db.collection.update(

   <query>,

   <update>,

   {

     upsert: <boolean>,

     multi: <boolean>,

     writeConcern: <document>,

     collation: <document>,

     arrayFilters: [ <filterdocument1>, ... ]

   }

)

参数讲解:

参数讲解:

   query:更新的选择条件。可以使用与find()方法中相同的查询选择器。

   update:要应用的修改。有关详细信息,请参见更新参数。

   upsert:可选的。如果设置为真,则在没有文档匹配查询条件时创建一个新文档。默认值为false,当没有找到匹配项时,不插入新文档。

   multi:可选的。如果设置为true,则更新满足查询条件的多个文档。如果设置为false,则更新一个文档。默认值为false。有关其他信息,请参见多参数。

   writeConcern:可选的。表达写作关注的文件。忽略使用默认的写关注点。

   collation:可选的。指定操作要使用的排序规则。排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号的规则

示例:

db.myColl.insertMany([{ _id: 1, category: "café", status: "A" },

{ _id: 2, category: "cafe", status: "a" },

{ _id: 3, category: "cafE", status: "a" }])

db.myColl.update(

   { category: "cafe" },

   { $set: { status: "Updated" } },

   { collation: { locale: "fr", strength: 1 } }

);

   arrayFilters:可选的。筛选器文档的数组,确定为对数组字段进行更新操作而修改哪些数组元素,注意有些客户端由于driver版本比较低可能会不支持该语法

1.1. 示例

 

初始化数据:

db.arrayFiltersExample.insertMany([{

    "_id" : 1.0,

    "grades" : [

        95.0,

        92.0,

        90.0

    ]

},

{

    "_id" : 2.0,

    "grades" : [

        98.0,

        100.0,

        102.0

    ]

},

{

    "_id" : 3.0,

    "grades" : [

        95.0,

        110.0,

        100.0

    ]

}]

)

 

示例:

db.arrayFiltersExample.update(

   { grades: { $gte: 100 } },

   { $set: { "grades.$[data]" : 100 } },

   {

     multi: true,

     arrayFilters: [ { "data": { $gte: 100 } } ]

   }

)

具体参照官网:

https://docs.mongodb.com/v4.0/reference/method/db.collection.update/

2. updateOne

 

说明:

基于筛选器更新集合中的单个文档。

 

语法:

db.collection.updateOne(

   <filter>,

   <update>,

   {

     upsert: <boolean>,

     writeConcern: <document>,

     collation: <document>,

     arrayFilters: [ <filterdocument1>, ... ]

   }

)

 

参数讲解;

filter:更新的选择条件。可以使用与find()方法中相同的查询选择器,指定一个空文档{}来更新集合中返回的第一个文档。

update:要应用的修改。有关详细信息,请参见更新参数:https://docs.mongodb.com/v4.0/reference/operator/update/

    upsert:可选的。如果设置为真,则在没有文档匹配查询条件时创建一个新文档。默认值为false,当没有找到匹配项时,不插入新文档。

   writeConcern:可选的。表达写作关注的文件。忽略使用默认的写关注点。

collation:可选的。指定操作要使用的排序规则。排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号的规则

    arrayFilters:可选的。筛选器文档的数组,确定为对数组字段进行更新操作而修改哪些数组元素,注意有些客户端由于driver版本比较低可能会不支持该语法

 

 

2.1. 示例

 

示例:

初始化数据:

db.updateExample.insertMany([{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan" ,"chef":[{"name":"liming","phone":111},{"name":"test1","phone":123}], "grades" : [ 95.0, 92.0, 90.0],"material":{"remark":"test"}},

{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2,"chef":[{"name":"liming","phone":111},{"name":"test1","phone":123}], "grades" : [ 98.0, 100.0, 102.0] ,"material":{"remark":"test"}},

{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 0 ,"chef":[{"name":"liming","phone":111},{"name":"test1","phone":123}],"grades" : [ 95.0, 110.0, 100.0],"material":{"remark":"test"}}])

 

  

2.1.1. 更新单个对象document

 

db.updateExample.updateOne(

      { "name" : "Central Perk Cafe" },

      { $set: { "violations" : 3 } }

);

 

db.updateExample.updateOne(

      { "name" : "Pizza Rat's Pizzaria" },

      { $set: {"_id" : 4, "violations" : 7, "borough" : "Manhattan" } },

      { upsert: true }

   );

2.1.2. 更新数组document

 

db.updateExample.updateOne(

   { "_id": 3 },

   { $set: { "grades.$[data]" : 100 } },

   {

     arrayFilters: [ { "data": { $gte: 100 } } ]

   }

)

 

注意:data 是自定义的名称

2.1.3. 更新嵌入式数组对象document

 

db.updateExample.updateOne({"_id":3},{$set:{"chef.$[].name":"ffff"}})

2.1.4. 更新嵌入式单个对象document

 

db.updateExample.updateOne(

   { "_id": 3 },

   {

     $set: { "material.remark": "test5", status: "P" },

     $currentDate: { lastModified: true }

   }

)

3. updateMany

 

说明:

基于筛选器更新集合内的多个文档。

其他同updateOne

4. replaceOne

 

说明:

根据筛选器替换集合中的单个文档。

 

语法:

db.collection.replaceOne(

   <filter>,

   <replacement>,

   {

     upsert: <boolean>,

     writeConcern: <document>,

     collation: <document>

   }

)

 

参数讲解;

filter:更新的选择条件。可以使用与find()方法中相同的查询选择器,指定一个空文档{}来更新集合中返回的第一个文档。

replacement:替换文件。不能包含更新操作符。

    upsert:可选的。如果设置为真,则在没有文档匹配查询条件时创建一个新文档。默认值为false,当没有找到匹配项时,不插入新文档。

   writeConcern:可选的。表达写作关注的文件。忽略使用默认的写关注点。

collation:可选的。指定操作要使用的排序规则。排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号的规则

    arrayFilters:可选的。筛选器文档的数组,确定为对数组字段进行更新操作而修改哪些数组元素,注意有些客户端由于driver版本比较低可能会不支持该语法

4.1. 示例

 

db.updateExample.replaceOne(

   { "_id":1 },

   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }

)

上一篇 下一篇

猜你喜欢

热点阅读