**使用了不存在的对象,即创建该对象use db 使用db数据库 show dbs 查看当前服务器中写在磁盘上的数据库 show tables 查看数据库中的collection db 查看当前使用的数据库1.增删改查: 增:db.collection.insert({数据}) 自动生成 _id : ObjectId("")官方推荐:db.collection.insertOne({数据}) 插入一条数据db.collection.insertMany([{数据},{数据}]) 插入多条数据 查:db.collection.find({条件})db.collection.findOne({条件}) 改:db.collection.update({条件},{$修改器:{数据}})官方推荐:db.collection.updateOne({条件},{$修改器:{数据}}) 更新一条数据db.collection.updateMany({条件},{$修改器:{数据}}) 更新所有数据 删:db.collection.remove({条件})官方推荐:db.collection.deleteOne({条件}) 删除一条数据db.collection.deleteMany({条件}) 删除所有符合条件的数据清除collection:db.collection.drop()2.$关键字数学比较符:$lt$lte$gt$gte$eq :查询关键字:$or db.collection.find({$or:[{name:1},{age:73}]})$in db.collection.find({age:{$in:[1,2,3,4]}}) #符合其中一个条件即可$all db.collection.find({hobby:{$all:[1,2,3,4]}}) #子集查询----------------------------------------- 1.$修改器 :$set 简单粗暴 {name:value} dict["name"]=value $unset 简单粗暴的删除字段 {$unset:{name:1}} del dict["name"]db.user_info.updateOne({age:200},{$unset:{age:1}})$inc 引用增加db.user_info.updateMany({},{$inc:{age:1}})array操作$push 在array中追加一个新的元素 [].append(item)db.user_info.updateOne({name:"200wansui"},{$push:{hobby:10}})$pull 在array中删除一个的元素 [].remove(item) [].pop(-1)db.user_info.updateOne({name:"200wansui"},{$pull:{hobby:0}})$pop 不含索引 -1 从前往后 1 从后往前db.user_info.updateOne({name:"200wansui"},{$pop:{hobby:1}})2.$ 字符db.user_info.updateOne({hobby:6},{$set:{"hobby.$":"六"}})保存符合索引条件数据的下标3.Object 字典操作db.user_info.updateOne({name:"200wansui"},{$inc:{"info.tizhong":-5}})db.user_info.updateOne({name:"200wansui"},{$set:{"info.long":12.5}})4.array + Objectdb.user_info.updateOne({"hobby.shengao":150},{$set:{"hobby.$.long":14}})5.limit db.user_info.find({}).limit(5)选取数据从当前位置选择5个6.skip 跳过db.user_info.find({}).skip(2) 从0开始跳过2条数据为当前位置7.sortdb.user_info.find({}).sort({ id:-1 })根据ID进行排序 -1倒叙 1正序8.limit+skip+sortdb.user_info.find({}).limit(5).skip(10)db.user_info.find({}).limit(c).skip((p-1)*c)db.user_info.find({}).limit(5).skip(5).sort({ id:-1 })优先级最高的是 sort 其次优先为 skip最低优先级 limit
总结
mongodb中的update修改器: $inc $set $unset $push $pull $inc : 对查询的结果对原有的基础上增加或者减少 db.user_info.updateOne({age:27},{$inc:{age:1}}) db.user_info.updateOne({age:28},{$inc:{age:-8}}) $set: 直接替换原来的值, 对原有的数据增加一个field db.user_info.updateOne({},{$set:{test_list:[1,2,3,4,5,6,7,8]}}) db.user_info.updateOne({age:20},{$set:{age:21}}) $unset: 用来删除key(field)的 db.user_info.updateOne({id:1},{$unset:{name:1}}) $push: 用来对Array(list)数据类型进行增加新元素,相当于append方法 db.user_info.updateOne({id:1},{$push:{hobby:3}}) $pull:对内部的数据进行删减, 删除array中的某一个元素 db.user_info.updateOne({id:1},{$pull:{test_list:8}}) $pop:指定删除array中的第一个或者最后一个元素, -1代表最前面, 1代表最后面 db.user_info.updateOne({id:1},{$pop:{test_list:1}}) $字符: 保存符合索引条件数据的下标 db.user_info.updateOne({test_list:4},{$set:{"test_list.$":"四"}}) object字典的操作 db.user_info.updateOne({id:1},{$set:{"info.height":161}}) db.user_info.updateOne({id:1},{$inc:{"info.weight":-7}}) array+object的操作 db.user_info.updateOne({"hobby.weight":100},{$set:{"hobby.$.height":164}}) limit +skip +sort db.user_info.find().limit(2).skip(1).sort({ id:-1 }) 重点: limit +skip +sort执行是有优先级别的, sort>skip>limitskip + limit的优先级: 先skip再limit