mongo -- python3

mongodb update : 修改器
update ( query,update,upset,multi)
query : 相当于 mysql 中的 where
update:要修改的域
upset:当没有这个值是。是否要建立新的域 true为建立 false为不建立
multi 为是否同时修改多个文档 为true 则 同时修改 false 只修改一个

1.$set

e.g. db.class.update({"age":20},{$set:{"sex":"m"}},false,true)
修改匹配到的年龄为20的文档将一条文档的性别sex改为"m"

set 同时可以添加新的域

2.$unset

e,g. db.class.update({"age":20},{$unset:{"sex":0}},false,false)
删除匹配到 年龄为20一个的性别域

3.$rename

e.g. db.class.update({"age":20},{$rename:{"age":"gender"}},false,false)
将一条年龄为20的文档的age改名为gender

4.$inc

db.class.update({"age":20},{$inc:{"age":-1}},false,false)
将匹配到的一条年龄为20的文档的年龄增加1

$inc 中 整数 为增加 负数为减少

5.$mul

db.class.update({"age":20},{$mul:{"age":1}},false,false)
将匹配到的一条文档的 age 进行×1 操作

$min 设定最小值

db.class.update({"age":20},{$min:{"age":20}},false,false)

当文档age 的值小于20 时则不做修改 当大于20时 则改变成最小值min 所设定的值

6.$max

db.class.update({"age":20},{$max:{"age":22}},false,false)

跟min 相反 当值小于设定的max 值时 则将那个值变成 max 对应的值

修改起可以一起使用

db.class.update({"age":20},{$max:{"age":20},$inc:{"class":1}},false,false)

同时进行修改

未完待续----------

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章