您的位置:首页 > 数据库 > Mongodb

mongodb-更新操作符

2015-09-22 13:30 330 查看

更新操作符

1. $set 修改字段

更新字段值。

也可以改变字段值的类型。若字段不存在,则自动创建。

1.1. 字段存在:更新字段值
字段存在,更新字段值

db.collection.update({},{"$set":{"operateDate":"2015-07-07"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-07-07",

}

1.2. 字段不存在:创建字段并赋值
字段不存在,创建字段并赋值

db.collection.update({},{"$set":{"flag":"001"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16"

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"flag" : "001"

}

1.3. 修改字段类型
赋值的过程中可以修改字段类型

db.collection.update({},{"$set":{"operateDate":20150707}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16"

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : 20150707

}

1.4. 修改字段内嵌文档
对于字段内文档使用"."符号连接多集字段。

使用"."符号连接多集字段

db.collection.update({},{"$set":{"indoor.open":99}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"indoor" : {

"open" : 1,

"floor" : null

}

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"indoor" : {

"open" : 99,

"floor" : null

}

}

1.5. 修改多组值中的指定组
当指定字段存在多组值时,通过数字来确定指定组。0表示第一组。

使用"."符号连接多集字段

db.collection.update({},{"$set":{"brands.0.name":"中国脑残"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"brands" : [

{

"code" : "6401",

"name" : "中国移动"

}

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"brands" : [

{

"code" : "6401",

"name" : "中国脑残"

}

]

}

使用"."符号连接多集字段

db.collection.update({},{"$set":{"brands.1.name":"中国脑残"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"brands" : [

{

"code" : "6401",

"name" : "中国移动"

},

{

"code" : "6402",

"name" : "中国联通"

},

{

"code" : "6403",

"name" : "中国电信"

}

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"brands" : [

{

"code" : "6401",

"name" : "中国移动"

},

{

"code" : "6402",

"name" : "中国脑残"

},

{

"code" : "6403",

"name" : "中国电信"

}

]

}

2. $unset 删除字段

删除字段。

指定字段”:”后跟随的值可以是任何数字及字符串,并无实际意义。

2.1. 删除指定字段
db.collection.update({},{"$unset":{"operateDate":1}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16"

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2

}

2.2. 删掉多级字段的子字段
db.collection.update({},{"$unset":{"indoor.open":1}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"indoor" : {

"open" : 1,

"floor" : null

}

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"indoor" : {

"floor" : null

}

}

2.3. 删掉多组值中的指定组
删除指定组内的指定字段

db.collection.update({},{"$unset":{"brands.0.name":1}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"brands" : [

{

"code" : "6401",

"name" : "中国移动"

}

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"brands" : [

{

"code" : "6401"

}

]

}

删除指定组,将该组中的内容置为null

db.collection.update({},{"$unset":{"brands.0":1}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"brands" : [

{

"code" : "6401",

"name" : "中国移动"

},

{

"code" : "6402",

"name" : "中国联通"

},

{

"code" : "6403",

"name" : "中国电信"

}

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"brands" : [

null,

{

"code" : "6402",

"name" : "中国联通"

},

{

"code" : "6403",

"name" : "中国电信"

}

]

}

3. $rename 修改字段

修改字段名。

可以同时修改多个字段名。

db.collection.update({},{$rename:{"operateDate":"D","chargingPole":"P"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

" chargingPole " : []

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"D" : "2015-04-16",

"P" : []

}

4. $push 追加字段值

添加字段及其字段值。

当字段存在时,增加字段值。

需要注意的是新增的字段对应的字段值以list的形式出现。且不会对list中的值去重。

4.1. 字段存在:追加字段值
只能对字段值为list的字段追加字段值。

如例子中不可对字段” operateDate”赋值,错误提示如下:

The field 'operateDate' must be an arraybut is of type String in document {_id: ObjectId('55309f113769e949a093d590')}

字段存在:追加字段值

db.collection.update({},{"$push":{"chargingPole":"K"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F"

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K"

]

}

追加字段值时不会考虑去重。

字段存在:字段值存在,追加字段值

db.collection.update({},{"$push":{"chargingPole":"K"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K"

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K",

"K"

]

}

4.2. 字段不存在:创建字段并赋值
新增的字段对应的字段值以list的形式出现。

字段不存在:创建字段并赋值

db.collection.update({},{"$push":{"level":1}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : []

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [],

"level" : [

1

]

}

5. $pushAll 追加字段值

不同于$push,$pullAll可以一次追加到数组内多个值。

追加多个字段值

db.collection.update({},{"$pushAll":{"chargingPole":["F","K"]}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : []

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K"

]

}

6. $addToSet 追加字段值

增加一个值到list,当且仅当这个值不在list内。

由于$push在追加值时不考虑是否和现有的值重复。所以为了避免字段内的值重复。修改器$addToSet在$push基础上考虑了去重问题。

字段值存在:不追加

db.collection.update({},{"$addToSet":{"chargingPole":"K"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K"

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K"

]

}

字段值不存在:追加

db.collection.update({},{"$addToSet":{"chargingPole":"Q"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K"

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K",

"Q"

]

}

7. $pull 删除字段值

删除指定字段的指定值。

删除指定字段值中所有(可以为多个)和指定字段相同的值。

当指定字段不存在时不报错。

删除指定字段的指定值

db.collection.update({},{"$pull":{"chargingPole":"K"}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K",

"K"

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F"

]

}

8. $pullAll 删除字段值

不同于$pull,$pullAll可以一次删除数组内多个值。

删除多个字段值

db.collection.update({},{"$pullAll":{"chargingPole":["F","K"]}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"F",

"K",

"K"

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : []

}

9. $pop 删除字段值

删除指定字段的值。

只能从list的头或者尾删除元素。

当字段值为零、整数、字符串时:0,1,100,”K”,会从末尾删除元素

当字段值为负数:-1,-100,会从开头删除元素

从未尾删除元素

db.collection.update({},{"$pop":{"chargingPole":1}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"A1",

"A2",

"A3",

"A4",

"A5"

]

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"A1",

"A2",

"A3",

"A4"

]

}

从开头删除元素

db.collection.update({},{"$pop":{"chargingPole":-1}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"A1",

"A2",

"A3",

"A4",

"A5"

],

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"operateDate" : "2015-04-16",

"chargingPole" : [

"A2",

"A3",

"A4",

"A5"

]

}

10. $inc 修改字段值

对字段的值增加指定值。当指定值为负数是相当于减法。只能修改数字类型。

db.collection.update({},{"$inc":{"indoor.open":3}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"indoor" : {

"open" : 1,

"floor" : null

}

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"indoor" : {

"open" : 4,

"floor" : null

}

}

db.collection.update({},{"$inc":{"indoor.open":-3}})

处理前

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"indoor" : {

"open" : 1,

"floor" : null

}

}

处理后

{

"_id" : ObjectId("55309f113769e949a093d590"),

"id" : 2,

"indoor" : {

"open" : -2,

"floor" : null

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: