时间:2021-07-01 10:21:17 帮助过:26人阅读
> db.stutent.find() }) { "_id" : ObjectId("5d2ecd3d14ff51d814e40361"), "name" : "henry", "age" : 16, "sex" : "female", "hobby" : "喝酒" } { "_id" : ObjectId("5d2eeb8414ff51d814e40362"), "name" : "henry", "sex" : "female", "hobby" : "喝酒" } { "_id" : ObjectId("5d2eeb8414ff51d814e40363"), "name" : "egon", "age" : 18 } { "_id" : ObjectId("5d2eec9314ff51d814e40364"), "name" : "alex", "age" : 50 } { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30 } { "_id" : ObjectId("5d2eee1314ff51d814e40366"), "name" : "500ml", "age" : 10 }
db.stutent.find({age:{$gt:30}}) # 查询结果 { "_id" : ObjectId("5d2eec9314ff51d814e40364"), "name" : "alex", "age" : 50 }
db.stutent.find({age:{$gte:30}}) # 查询结果 { "_id" : ObjectId("5d2eec9314ff51d814e40364"), "name" : "alex", "age" : 50 } { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30 }
db.stutent.find({age:{$lt:30}}) # 查询结果 { "_id" : ObjectId("5d2ecd3d14ff51d814e40361"), "name" : "henry", "age" : 16, "sex" : "female", "hobby" : "喝酒" } { "_id" : ObjectId("5d2eeb8414ff51d814e40363"), "name" : "egon", "age" : 18 } { "_id" : ObjectId("5d2eee1314ff51d814e40366"), "name" : "500ml", "age" : 10 }
db.stutent.find({age:{$lt:30}}) # 查询结果 { "_id" : ObjectId("5d2ecd3d14ff51d814e40361"), "name" : "henry", "age" : 16, "sex" : "female", "hobby" : "喝酒" } { "_id" : ObjectId("5d2eeb8414ff51d814e40363"), "name" : "egon", "age" : 18 } { "_id" : ObjectId("5d2eee1314ff51d814e40366"), "name" : "500ml", "age" : 10 }
> db.stutent.find({age:{$eq:30}}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30 } > db.stutent.find({age:30}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30 }
> db.stutent.find({age:{$ne:30}}) { "_id" : ObjectId("5d2ecd3d14ff51d814e40361"), "name" : "henry", "age" : 16, "sex" : "female", "hobby" : "喝酒" } { "_id" : ObjectId("5d2eeb8414ff51d814e40362"), "name" : "henry", "sex" : "female", "hobby" : "喝酒" } { "_id" : ObjectId("5d2eeb8414ff51d814e40363"), "name" : "egon", "age" : 18 } { "_id" : ObjectId("5d2eec9314ff51d814e40364"), "name" : "alex", "age" : 50 } { "_id" : ObjectId("5d2eee1314ff51d814e40366"), "name" : "500ml", "age" : 10 }
在此前的update中,我们用过$set,对数据进行过更新,其实在update中还存在很多的$关键字,我们把update中的这些关键字叫做 修改器
将查询结果加上某个值后保存,就是原有基础上在增加多少或减少多少
# 给名字为500ml人的年龄加上8 db.stutent.update({name:"500ml"},{$inc:{age:+8}}) # 给名字为500ml人的年龄减去8 db.stutent.update({name:"500ml"},{$inc:{age:-8}})
更新属性没有就添加一条
# 将名字为500ml的人名字改成大呲花 db.stutent.update({name:"500ml"},{$set:{name:"大呲花"}})
用来删除Key(field)的
# 删除名字henry中的sex为female的字段 db.stutent.update({name:"henry"},{$unset:{sex:"female"}})
针对 Array == list 操作
在Array的最后一个位置中增加数据
# 给小漩涡添加个字段,属性为列表 db.stutent.update({name:"小漩涡"},{$set:{test_list:[1,2,3,4,5]}}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "test_list" : [ 1, 2, 3, 4, 5 ] } # 可以为所有满足条件的 Document 添加 "test_list" db.stutent.update({},{$set:{test_list:[1,2,3,4,5]}}) # 注意我这里的条件为空 " {} " 就算是为空,也要写上"{}" 记住记住记住 # -------------------------------------------------------------------------- # 用push给test_list中追加元素 db.stutent.update({name:"小漩涡"},{$push:{test_list:1000}}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "test_list" : [ 1, 2, 3, 4, 5, 1000 ] }
迭代追加
> db.stutent.find({name:"小漩涡"}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "hobby" : [ "喝酒", "烫头" ] } > db.stutent.update({name:"小漩涡"},{$pushAll:{hobby:[1,2,3,4,5]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.stutent.find({name:"小漩涡"}) }}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "hobby" : [ "喝酒", "烫头", 1, 2, 3, 4, 5 ] }
删除Array中的指定元素
db.stu.update({name:"小黑"},{ $pull: { hoobys:"渣男" } })
删除Array中的多个元素
> db.stutent.find({name:"小漩涡"}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "hobby" : [ "喝酒", "烫头", 1, 2, 3, 4, 5 ] } > db.stutent.update({name:"小漩涡"},{$pullAll:{hobby:[1,2,3,4,5]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.stutent.find({name:"小漩涡"}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "hobby" : [ "喝酒", "烫头" ] }
$pop约等于pop()
删除Array中的第一个或者最后一个元素 正数是倒序删除 负数是正序删除
> db.stutent.find({name:"小漩涡"}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "test_list" : [ 1, 2, 3, 4, 5, 1000 ], "hobby" : [ "喝酒", "烫头" ] } > db.stutent.update({name:"小漩涡"},{$pop:{hobby:-1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.stutent.find({name:"小漩涡"}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "test_list" : [ 1, 2, 3, 4, 5, 1000 ], "hobby" : [ "烫头" ] }
在MongoDB中有一个非常神奇的符号 "$"
"$" 在 update 中 加上关键字 就 变成了 修改器
其实 "$" 字符 独立出现也是有意义的 , 我起名叫做代指符
存储当前(Array)符合条件的元素下标索引 ,只能存储最外层的 索引位置
> db.stutent.find({name:"小漩涡"}) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "test_list" : [ 1, 2, 3, 4, 5, 1000 ], "hobby" : [ "烫头" ] } # 将test_list中的5改成五 # "test_list.$"必须加引号 > db.stutent.update({test_list:5},{$set:{"test_list.$":"五"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.stutent.find({name:"小漩涡"}) ) ) { "_id" : ObjectId("5d2eee1314ff51d814e40365"), "name" : "小漩涡", "age" : 30, "test_list" : [ 1, 2, 3, 4, "五", 1000 ], "hobby" : [ "烫头" ] } # 先查询到hobbys中5的 位置(4) ,将位置存储在 $==(4) 字符中 然后根据$字符的位置(4)更改数据
MongoDB-比较符及修改器
标签:mongod array 位置 ted == 负数 大于 一个 rem