MongoDB
MongoDB 是一个开源的文档数据库,并领先的 NoSQL 数据库。 MongoDB 是由 c++语言编写。
文档
文档是一组键 - 值对。文件动态模式。动态模式是指,在相同集合中的文档不需要具有相同的字段或结构组的公共字段的集合的文档,可以容纳不同类型的数据。
db.集合名称.update({query},{update},upsert, multi})
query:过滤条件
update:修改内容
upsert:如果不存在查询条件查出的记录,是否插入一条数据,默认是false
multi:是否只修改查询条件查出的第一条记录,默认是false
> db.student.update({_id:1}, {name:"zhang"}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhang" } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 }
把整条数据都修改了,如果修改内容中没有设置age字段,则以前的age也取消了,数据全部修改
局部修改
$set:如果存在键则修改,如果不存在键则添加
> db.student.update({_id: 1},{$set:{name:"zhangsan", age: 26}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 }
id重复时无法修改
> db.student.update({_id:1},{_id:0, name:"zhangsanzhangsan"}) WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16837, "errmsg" : "The _id field cannot be changed from {_id: 1.0} to {_id: 0.0}." } }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 }
第三个参数默认为false,如果查不到数据false时不执行任何操作,true时往数据库里新插入一条数据
> db.student.update({_id:7},{_id:7, name: "songjiu", age:32}) WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 }) > db.student.update({_id:7},{_id:7, name: "songjiu"}, true) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 7 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "age" : 32 }
第四个参数默认为false,false时表示只修改查询到的第一条数据,true时表示修改查询到的所有数据
> db.student.update({age:32}, {$set:{age:33}}, false, false) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 33 } { "_id" : 7, "name" : "songjiu", "age" : 32 } > db.student.update({age:33}, {$set:{age:32}}, false, true) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 32 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "age" : 32 } > db.student.update({age:32}, {$set:{age:33}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "age" : 32 }
$inc:在原来的基础上加多少
> db.student.update({_id:7}, {$inc:{age:-1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "age" : 31 }
$unset:去掉某个键
> db.student.update({_id:7},{$unset:{age:1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu" }
针对数组类型的操作
$push:当数据中不存在键时,创建数组类型的键并插入该值;如果存在该键,并且该键是数组类型时,则在此数组类型的数据上追加;如果存在该键,并且该键不是数组类型时,会报错。
$pushAll:批量往数组中追加
$addToSet:数组中有该值时不追加,没有该值时追加
> db.student.update({_id: 7},{$push:{skill:"java"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "java" ] } > db.student.update({_id: 7},{$push:{skill:"mongodb"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "java", "mongodb" ] } > db.student.update({_id: 7},{$push:{name:"111"}}) WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16837, "errmsg" : "The field 'name' must be an array but is of type String in document {_id: 7.0}" } }) > db.student.update({_id: 7},{$pushAll:{skill:["js","C++","java"]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "java", "mongodb", "js", "C++", "java" ] } > db.student.update({_id:7},{$addToSet:{skill:"mongodb"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "java", "mongodb", "js", "C++", "java" ] }
$pop:删除数组的第一个或最后一个元素,值为-1时是删除第一个元素,值为1时是删除最后一个元素。
$pull:删除数组中的某一个指定的数值
$pullAll:删除数组中多个指定的数值
> db.student.update({_id:7},{$pop:{skill:1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "java", "mongodb", "js", "C++" ] } > db.student.update({_id:7},{$pop:{skill:-1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "js", "C++" ] } > db.student.update({_id:7},{$pull:{skill:"js"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "C++" ] } > db.student.update({_id:7},{$pullAll:{skill:["js"]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ "mongodb", "C++" ] } > db.student.update({_id:7},{$pullAll:{skill:["mongodb","C++"]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : 1, "name" : "zhangsan", "age" : 26 } { "_id" : 2, "name" : "lisi", "age" : 27 } { "_id" : 3, "name" : "wangwu", "age" : 30 } { "_id" : 4, "name" : "zhaoliu", "age" : 28 } { "_id" : 5, "name" : "qianliu", "age" : 33 } { "_id" : 6, "name" : "sunba", "age" : 32 } { "_id" : 7, "name" : "songjiu", "skill" : [ ] }
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]