python操作mongodb数据库
Posted 嶙羽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python操作mongodb数据库相关的知识,希望对你有一定的参考价值。
MongoDB简介
MongoDB是一种文档型的非关系型数据库(NoSQL),举例如下:
{“foo”:3,"greeting":"Hello,world!"}
并且区分大小写,key值唯一,不可重复,文档可以嵌套,键值对是有序的
集合
1、集合就是一组文档
2、文档类似于关系库里的行
3、集合类似于关系库里的表
4、集合里的文档无需固定的结构(与关系型数据库的区别)
集合的命名
1、不能是空字符串
2、不能包含\\0字符(空字符)
3、不能使用system.的前缀
4、建议不包含保留字“$”
5、用.分割不同命名空间的子集合(如:blog.users)
数据库
1、多个文档组成集合,多个集合组成数据库
2、一个实例可以承载多个数据库
3、每个数据库都有独立的权限
4、保留的数据库名称(admin,local,config)
命令行操作MongoDB
参考菜鸟教程
在\\MongoDB\\Server\\3.4\\bin进入命令行窗口,然后敲mongo即进入MongoDB服务,显示所有数据库
> show dbs;
使用某一数据库(如果不存在的话则会自动新建)
> use 数据库名称;
插入数据
写数据
> stu = { name:\'Jhon\', age:21, "sex":"male" }
插入数据
> db.数据库名称.insert(stu)
查询数据
> db.数据库名称.find()
更新数据
> stu_obj = db.students.findOne() { "_id" : ObjectId("5a9a01c5b1489e70f3a89a0c"), "name" : "Jhon", "age" : 21, "sex" : "male" } > stu_obj.name = "Jackchen" Jackchen > db.students.update({name:"Jhon"},stu_obj) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.students.findOne() { "_id" : ObjectId("5a9a01c5b1489e70f3a89a0c"), "name" : "Jackchen", "age" : 21, "sex" : "male" }
更新数据时,update里第二个参数会完全代替前一个参数,所以如果第二个参数只有{name:"**"},会把原来的数据删除
如果要更新多个数据,则可以使用updateMany
总结:这种方式更新数据相当麻烦,可以用修改器
删除数据
原来是三条数据:
> db.students.find() { "_id" : ObjectId("5a9a01c5b1489e70f3a89a0c"), "name" : "Jhon", "age" : 21, "sex" : "male" } { "_id" : ObjectId("5a9a04d9b1489e70f3a89a0d"), "name" : "oldhu" } { "_id" : ObjectId("5a9a09a3b1489e70f3a89a0e"), "name" : "Amy", "age" : 16, "sex" : "male" }
删除其中一条:
> db.students.remove({name:"oldhu"}) WriteResult({ "nRemoved" : 1 }) > db.students.find() { "_id" : ObjectId("5a9a01c5b1489e70f3a89a0c"), "name" : "Jackchen", "age" : 21, "sex" : "male" } { "_id" : ObjectId("5a9a09a3b1489e70f3a89a0e"), "name" : "Amy", "age" : 16, "sex" : "male" }
删除全部:
> db.students.remove({}) WriteResult({ "nRemoved" : 2 }) > db.students.find() >
练习
1、创建一个学生信息表(包含:姓名,性别,成绩,年龄)
2、写入十五条不同的数据
3、查询所有的男生数据(只需要学生的姓名和年龄)
4、查询成绩合格的学生信息(学生成绩大于或等于60分)
5、查询所有18岁的男生和16岁的女生的数据
6、按照学生的年龄进行排序
7、将所有的学生年龄增加一岁
步骤1和2、
先在文本编辑器里面编辑好文档,推荐使用sublimeText3或者Notepad++
> use students;
> db.students.insertMany( ... [ ... {name:"bob",age:14,sex:"male",grade:95}, ... {name:"ahn",age:18,sex:"female",grade:45}, ... {name:"xi",age:15,sex:"male",grade:75}, ... {name:"hiy",age:23,sex:"male",grade:85}, ... {name:"young",age:21,sex:"male",grade:45}, ... {name:"nive",age:25,sex:"female",grade:55}, ... {name:"longsi",age:19,sex:"male",grade:95}, ... {name:"puki",age:16,sex:"female",grade:95}, ... {name:"bbc",age:26,sex:"male",grade:95}, ... {name:"hippop",age:54,sex:"male",grade:95}, ... {name:"sue",age:21,sex:"feale",grade:95}, ... {name:"yang",age:36,sex:"male",grade:95} ... ] ... ) { "acknowledged" : true, "insertedIds" : [ ObjectId("5a9a10adb1489e70f3a89a0f"), ObjectId("5a9a10adb1489e70f3a89a10"), ObjectId("5a9a10adb1489e70f3a89a11"), ObjectId("5a9a10adb1489e70f3a89a12"), ObjectId("5a9a10adb1489e70f3a89a13"), ObjectId("5a9a10adb1489e70f3a89a14"), ObjectId("5a9a10adb1489e70f3a89a15"), ObjectId("5a9a10adb1489e70f3a89a16"), ObjectId("5a9a10adb1489e70f3a89a17"), ObjectId("5a9a10adb1489e70f3a89a18"), ObjectId("5a9a10adb1489e70f3a89a19"), ObjectId("5a9a10adb1489e70f3a89a1a") ] }
> db.students.find() { "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 18, "sex" : "female", "grade" : 45 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 21, "sex" : "male", "grade" : 45 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 25, "sex" : "female", "grade" : 55 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95 }
步骤3、
> db.students.find({sex:\'male\'},{name:1,age:1,_id:0}) { "name" : "bob", "age" : 14 } { "name" : "xi", "age" : 15 } { "name" : "hiy", "age" : 23 } { "name" : "young", "age" : 21 } { "name" : "longsi", "age" : 19 } { "name" : "bbc", "age" : 26 } { "name" : "hippop", "age" : 54 } { "name" : "yang", "age" : 36 }
步骤4、
> db.students.find({grade:{\'$gte\':60}}) { "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95 }
步骤5、
> db.students.find({\'$or\':[{sex:\'male\',age:18},{sex:\'female\',age:16}]}) { "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 }
步骤6、(升序)
> db.students.find().sort({age:1}) { "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 18, "sex" : "female", "grade" : 45 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 21, "sex" : "male", "grade" : 45 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 25, "sex" : "female", "grade" : 55 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95 }
(降序)
> db.students.find().sort({age:-1}) { "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 25, "sex" : "female", "grade" : 55 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 21, "sex" : "male", "grade" : 45 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 18, "sex" : "female", "grade" : 45 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75 } { "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95 }
步骤7、(使用修改器来更新)
例子:将所有集合增加一个address项,值是‘---’
> db.students.update({},{\'$set\':{address:\'---\'}},{multi:true}) WriteResult({ "nMatched" : 12, "nUpserted" : 0, "nModified" : 12 }) > db.students.find() { "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 18, "sex" : "female", "grade" : 45, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 21, "sex" : "male", "grade" : 45, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 25, "sex" : "female", "grade" : 55, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95, "address" : "---" }
所有学生年龄增加一岁:
> db.students.update({},{\'$inc\':{age:1}},{multi:true}) WriteResult({ "nMatched" : 12, "nUpserted" : 0, "nModified" : 12 }) > db.students.find() { "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 15, "sex" : "male", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 19, "sex" : "female", "grade" : 45, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 16, "sex" : "male", "grade" : 75, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 24, "sex" : "male", "grade" : 85, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 22, "sex" : "male", "grade" : 45, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 26, "sex" : "female", "grade" : 55, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 20, "sex" : "male", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 17, "sex" : "female", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 27, "sex" : "male", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 55, "sex" : "male", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 22, "sex" : "feale", "grade" : 95, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 37, "sex" : "male", "grade" : 95, "address" : "---" }
所有女学生年龄增加一岁:
> db.students.update({sex:"female"},{\'$inc\':{age:1}},{multi:true}) WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 }) > db.students.find({sex:"female"}) { "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 20, "sex" : "female", "grade" : 45, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 27, "sex" : "female", "grade" : 55, "address" : "---" } { "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 18, "sex" : "female", "grade" : 95, "address" : "---" }
MongoDB数据库可视化工具
这里用的是Robo 3T,可以方便查看数据库,以及实现增删改查操作
以上是关于python操作mongodb数据库的主要内容,如果未能解决你的问题,请参考以下文章
100天精通Python(进阶篇)——第40天:pymongo操作MongoDB数据库基础+代码实战