Home > Database > Mysql Tutorial > body text

MongoDB操作手册CRUD更新update

WBOY
Release: 2016-06-07 16:06:20
Original
944 people have browsed it

修改记录 概述 MongoDB提供了update()方法用于更新记录。这个方法接受以下参数: 一个更新条件的JSON对象用于匹配记录,一个更新操作JSON对象用于声明更新操作,和一个选项JSON对象 声明查询条件,使用和查询一样的结构和语法。 默认情况下,update()更新单

修改记录

概述

MongoDB提供了update()方法用于更新记录。这个方法接受以下参数:
一个更新条件的JSON对象用于匹配记录,一个更新操作JSON对象用于声明更新操作,和一个选项JSON对象
声明查询条件,使用和查询一样的结构和语法。
默认情况下,update()更新单条记录,若要更新多条记录, 请使用multi选项。

更新记录中的指定字段

用于更新某个字段的某个值,MongoDB提供了update操作符,比如$set。
在执行更新操作时,一些操作符回创建没有的字段,如$set。
测试数据:db.testData.insert({item:'MON2'});

1.使用update操作符来更新字段值

对于某条记录,其中item字段值为MNO2,使用$set操作符来更新它的category和details字段,使用$currentDate操作符来更新lastModified字段
db.testData.update(
{item:'MNO2'},
{
$set:{
category:'apparel',
details:{model:'14Q3',manufacturer:'XYZ Company'}
},
$currentDate:{lastModified:true}
}
);
这个更新操作返回包含着操作状态的WriteResult对象。
一个成功的更新操作返回如下结果:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
nMatched总段表明匹配的记录数,nModified字段表明修改的记录数。

2.更新内嵌字段

使用"."操作符,并将这个属性用引号括起来。

例:更新内嵌的model的details字段

db.testData.update({item:'ABC1'},{$set:{'details.model':'14Q2'}});

3.更新多条记录

更新包含category,值为”clothing“的所有记录的category值为“apparel”,lastModified字段为当前时间
db.testData.update(
{category:'clothing'},
{
$set:{category:'apparel'},
$currentDate:{lastModified:true}
},
{multi:true}
)

替换记录

除_id字段外,替换一条记录的所有内容,需要将新的整个记录的对象作为update()方法的第二个参数传进来。
替换的记录可以与之前的记录有着不同的字段,由于_id字段是 不可变的,在替换记录中可以省略_id字段。如果非要包含该字段,必须是集合中存在的一个值。
例:替换item字段为BE10的记录。替换后新的记录将只包含_id字段和替换记录中的字段。
db.testData.update(
{ item: "BE10" },
{
item: "BE05",
stock: [ { size: "S", qty: 20 }, { size: "M", qty: 5 } ],
category: "apparel"
}
)

upsert选项

概述

默认情况下,如果update()方法中没有匹配记录,这个方法将不执行任何操作。

但是,如果声明upsert:true,在有匹配记录的时候将进行更新,没有匹配的时候将进行插入操作。

1.替换记录时使用upsert

当使用update操作来替换一条记录的时候声明upsert:true,如果没有匹配的记录,MongoDB将使用update中的 查询条件创建一条新的记录,然后使用update中的用于替换的记录来替换新创建的记录的除_id字段外的所有内容。
db.testData.update(
{ item: "TBD1" },
{
item: "TBD1",
details: { "model" : "14Q4", "manufacturer" : "ABC Company" },
stock: [ { "size" : "S", "qty" : 25 } ],
category: "houseware"
},
{ upsert: true }
)
结果:nUpserted“:1表明
WriteResult({
"nMatched" : 0,//没有匹配记录
"nUpserted" : 1,//插入了一条新的记录
"nModified" : 0,//没有更新记录
"_id" : ObjectId("53dbd684babeaec6342ed6c7")//新插入的记录的_id
})

2.更新记录时使用upsert

当使用update操作来更新一条记录的时候声明upsert:true,同上
db.testData.update(
{ item: "TBD2" },
{
$set: {
details: { "model" : "14Q3", "manufacturer" : "IJK Co." },
category: "houseware"
}
},
{ upsert: true }
)
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!