Mongodb学习笔记(更新)二
db.students.insert({name:"张三",school:{name:"清华大学", city:"北京"}, age:19, gpa:3.97})
db.students.insert({name:"李四",school:{name:"北京大学", city:"北京"}, age:20, gpa:3.3})
db.students.insert({name:"王二",school:{name:"交通大学", city:"上海"}, age:22, gpa:3.68})
db.students.insert({name:"小牛",school:{name:"哈工大", city:"哈尔滨"}, age:21, gpa:3.50})
db.students.insert({name:"小马",school:{name:"交通大学", city:"西安"}, age:21, gpa:3.70})
db.students.insert({name:"小朱"})
db.students.insert({name:"小朱"})
# 从表中删除记录
> db.students.find({school:{$exists:false}})
{ "_id" : ObjectId("5bb9cdc4b8dd7ec496450222"), "name" : "小朱" }
{ "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱" }
> db.students.remove({ "_id" : ObjectId("5bb9cdc4b8dd7ec496450222")})
WriteResult({ "nRemoved" : 1 })
# 查询过滤没有提供school信息的学生
> db.students.find({school:{$exists:false}})
{ "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱" }
>
> db.students.find()
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "name" : "张三", "school" : { "na
me" : "清华大学", "city" : "北京" }, "age" : 19, "gpa" : 3.97 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "na
me" : "北京大学", "city" : "北京" }, "age" : 20, "gpa" : 3.3 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "na
me" : "交通大学", "city" : "上海" }, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "na
me" : "哈工大", "city" : "哈尔滨" }, "age" : 21, "gpa" : 3.5 }
>
> db.students.update({"_id" : ObjectId("5bb9d0ddb8dd7ec496450223")},{age:30,gpa:3.99})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find({"_id" : ObjectId("5bb9d0ddb8dd7ec496450223")})
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 30, "gpa" : 3.99 }
>
> db.students.find()
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 30, "gpa" : 3.99 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "na
me" : "北京大学", "city" : "北京" }, "age" : 20, "gpa" : 3.3 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "na
me" : "交通大学", "city" : "上海" }, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "na
me" : "哈工大", "city" : "哈尔滨" }, "age" : 21, "gpa" : 3.5 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "na
me" : "交通大学", "city" : "西安" }, "age" : 21, "gpa" : 3.7 }
# 如果更新的年龄谁大就更新谁
> db.students.update({"_id" : ObjectId("5bb9d0ddb8dd7ec496450223")},{$max:{age:38}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find()
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 38, "gpa" : 3.99 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "name" : "北京大学", "city" : "北京"
}, "age" : 20, "gpa" : 3.3 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学", "city" : "上海"
}, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大", "city" : "哈尔滨"
}, "age" : 21, "gpa" : 3.5 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学", "city" : "西安"
}, "age" : 21, "gpa" : 3.7 }
{ "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱" }
> db.students.update({"_id" : ObjectId("5bb9d0ddb8dd7ec496450223")},{$max:{age:32}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
>
> db.students.find()
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 38, "gpa" : 3.99 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "school" : { "name" : "北京大学", "city" : "北京"
}, "age" : 20, "gpa" : 3.3 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学", "city" : "上海"
}, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大", "city" : "哈尔滨"
}, "age" : 21, "gpa" : 3.5 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学", "city" : "西安"
}, "age" : 21, "gpa" : 3.7 }
{ "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱" }
# 去掉某个字段
> db.students.update({ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224")},{$unset:{school:true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find()
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 38, "gpa" : 3.99 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "age" : 20, "gpa" : 3.3 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学", "city" : "上海"
}, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大", "city" : "哈尔滨"
}, "age" : 21, "gpa" : 3.5 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学", "city" : "西安"
}, "age" : 21, "gpa" : 3.7 }
{ "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱" }
>
# 批量更新 让所有的年龄都增长1岁
> db.students.find()
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 38, "gpa" : 3.99 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "age" : 20, "gpa" : 3.3 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学", "city" : "上海"
}, "age" : 22, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大", "city" : "哈尔滨"
}, "age" : 21, "gpa" : 3.5 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学", "city" : "西安"
}, "age" : 21, "gpa" : 3.7 }
{ "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱" }
# $inc增长 multi:true 多条都增加, 不加这个只会增加遇到的第一条
> db.students.update({},{$inc:{age:1}},{multi:true})
WriteResult({ "nMatched" : 6, "nUpserted" : 0, "nModified" : 6 })
> db.students.find()
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 39, "gpa" : 3.99 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "age" : 21, "gpa" : 3.3 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学", "city" : "上海"
}, "age" : 23, "gpa" : 3.68 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大", "city" : "哈尔滨"
}, "age" : 22, "gpa" : 3.5 }
{ "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学", "city" : "西安"
}, "age" : 22, "gpa" : 3.7 }
{ "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱", "age" : 1 }
>
# 更新数据库中没有的的数据,如果有就更新,没有就新插入一条记录并更新 > db.students.find() { "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 39, "gpa" : 3.99 } { "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "age" : 21, "gpa" : 3.3 } { "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学", "city" : "上海" }, "age" : 23, "gpa" : 3.68 } { "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大", "city" : "哈尔滨" }, "age" : 22, "gpa" : 3.5 } { "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学", "city" : "西安" }, "age" : 22, "gpa" : 3.7 } { "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱", "age" : 1 }
> db.students.update({name:"Mike"},{$inc:{age:1},$setOnInsert:{school:{name:"new school"}}},{upsert:true}) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId("5bba03aa56f7ff5ab88c775c") }) > db.students.find() { "_id" : ObjectId("5bb9d0ddb8dd7ec496450223"), "age" : 39, "gpa" : 3.99 } { "_id" : ObjectId("5bb9d0ddb8dd7ec496450224"), "name" : "李四", "age" : 21, "gpa" : 3.3 } { "_id" : ObjectId("5bb9d0ddb8dd7ec496450225"), "name" : "王二", "school" : { "name" : "交通大学", "city" : "上海" }, "age" : 23, "gpa" : 3.68 } { "_id" : ObjectId("5bb9d0ddb8dd7ec496450226"), "name" : "小牛", "school" : { "name" : "哈工大", "city" : "哈尔滨" }, "age" : 22, "gpa" : 3.5 } { "_id" : ObjectId("5bb9d0ddb8dd7ec496450227"), "name" : "小马", "school" : { "name" : "交通大学", "city" : "西安" }, "age" : 22, "gpa" : 3.7 } { "_id" : ObjectId("5bb9d0dfb8dd7ec496450228"), "name" : "小朱", "age" : 1 } { "_id" : ObjectId("5bba03aa56f7ff5ab88c775c"), "name" : "Mike", "age" : 1, "school" : { "name" : "new school" } } >
本文来自博客园,作者:热爱技术的小牛,转载请注明原文链接:https://www.cnblogs.com/my-blogs-for-everone/articles/9750781.html

浙公网安备 33010602011771号