python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

时间:2020-12-02 19:40:12

python操作mongodb数据库①mongodb的安装和简单使用

参考文档:
中文版:http://www.mongoing.com/docs/crud.html
英文版:https://docs.mongodb.com/

环境准备:
1.下载mongodb for windows 2008r2版本,进行安装,建议安装在盘的根目录
比如安装在d:/mongodbserver目录下

安装完数据库以后可以设置环境变量,这样直接就可以在cmd命令行进行操作了

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

2.创建数据目录
D:\mongodbserver\data

3.cmd命令行下启动mongodb程序
D:\mongodbserver\bin>mongod --dbpath D:\mongodbserver\data

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

基本概念:
文档:区分大小写、key唯一不可重复、文档可嵌套、键值对是有序的

集合:集合就是一组文档,文档类似关系数据库里的行,集合类似于关系数据库里的表,集合中的文档无需固定的结构(与关系型数据库的区别)

集合的命名规范:
1.不能是空字符串("")
2.不能包含\0字符(空字符)
3.不能使用system.的前缀(系统保留)
4.建议不包含保留字"$"
5.用.分割不同命名空间的子集合(如:blog.users, blog.posts)

重点:
1.多个文档组成集合,多个集合组成数据库
2.一个实例可以承载多个数据库
3.每个数据库都有独立的权限
4.保留的数据库名称(admin,local,config)

基本操作:
cmd窗口敲入mongo回车进入命令行

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

1.函数的定义和使用

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

2.新增数据

> use students;
switched to db students
> stu = {"name":"jack","age":20 }
{ "name" : "jack", "age" : 20 }
> db.students.insert(stu)
WriteResult({ "nInserted" : 1 })

 插入单条、多条数据

>db.students.insertMany([{"name":"lucy","age":25},{"name":"lilei","age":30},{"name":"hanmeimei","age":28}])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5a9d58fbcfb5edc5e31235f4"),
                ObjectId("5a9d58fbcfb5edc5e31235f5"),
                ObjectId("5a9d58fbcfb5edc5e31235f6")
        ]
}
> db.students.find()
{ "_id" : ObjectId("5a9d5822cfb5edc5e31235f0"), "name" : "jack", "age" : 20 }
{ "_id" : ObjectId("5a9d5892cfb5edc5e31235f1"), "name" : "lily", "age" : 18 }
{ "_id" : ObjectId("5a9d58a3cfb5edc5e31235f2"), "name" : "tom", "age" : 25 }
{ "_id" : ObjectId("5a9d58cdcfb5edc5e31235f3"), "name" : "lucy", "age" : 25 }
{ "_id" : ObjectId("5a9d58fbcfb5edc5e31235f4"), "name" : "lucy", "age" : 25 }
{ "_id" : ObjectId("5a9d58fbcfb5edc5e31235f5"), "name" : "lilei", "age" : 30 }
{ "_id" : ObjectId("5a9d58fbcfb5edc5e31235f6"), "name" : "hanmeimei", "age" : 28 }

 

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

2.数据的查询
查询所有数据

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

3.查询单条数据

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

4.修改数据

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

5.删除数据

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

6.删除所有数据

> db.students.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 6 }
> db.students.find()

python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

练习任务:
1.创建一个学生信息表(至少包含:姓名、性别、成绩、年龄)
2.写入15条不同的数据

db.students.insertMany(
[
    {name: "bob", age:16, gender:"male", grade:95},
    {name: "ana", age:18, gender:"female", grade:45},
    {name: "xi", age:15, gender:"male", grade:75},
    {name: "bob1", age:16, gender:"male", grade:95},
    {name: "ana1", age:18, gender:"female", grade:45},
    {name: "jack", age:18, gender:"male", grade:85},
    {name: "tom", age:19, gender:"male", grade:65},
    {name: "lily", age:16, gender:"female", grade:59},
    {name: "lucy", age:18, gender:"female", grade:68},
    {name: "lilei", age:18, gender:"female", grade:68},
    {name: "hanmeimei", age:16, gender:"female", grade:90},
    {name: "harry", age:16, gender:"male", grade:81},
    {name: "json", age:16, gender:"male", grade:75},
    {name: "jim", age:16, gender:"male", grade:36},
    {name: "rose", age:16, gender:"female", grade:91},
    {name: "moli", age:16, gender:"female", grade:93},
    {name: "linda", age:16, gender:"female", grade:96}
]
)

 

3.查询所有的男生数据(只需要学生的姓名和年龄)
> db.students.find({gender:"male"},{age:true,name:1,_id:0})
{ "name" : "bob", "age" : 16 }
{ "name" : "xi", "age" : 15 }
{ "name" : "bob1", "age" : 16 }
{ "name" : "jack", "age" : 18 }
{ "name" : "tom", "age" : 19 }
{ "name" : "harry", "age" : 16 }
{ "name" : "json", "age" : 16 }
{ "name" : "jim", "age" : 16 }

4.查询成绩几个的学生信息(学生成绩大于或等于60分)
> db.students.find({grade :{$gte:60}})

5.查询所有18岁的男生和16岁女生的数据
> db.students.find({$or :[{gender:"female",age:16},{gender:"male",age:18}]})
{ "_id" : ObjectId("5a9df2e48a86b467d4a2c44e"), "name" : "jack", "age" : 18, "gender" : "male", "grade" : 85 }
{ "_id" : ObjectId("5a9df2e48a86b467d4a2c450"), "name" : "lily", "age" : 16, "gender" : "female", "grade" : 59 }
{ "_id" : ObjectId("5a9df2e48a86b467d4a2c453"), "name" : "hanmeimei", "age" : 16, "gender" : "female", "grade" : 90 }
{ "_id" : ObjectId("5a9df2e48a86b467d4a2c457"), "name" : "rose", "age" : 16, "gender" : "female", "grade" : 91 }
{ "_id" : ObjectId("5a9df2e48a86b467d4a2c458"), "name" : "moli", "age" : 16, "gender" : "female", "grade" : 93 }
{ "_id" : ObjectId("5a9df2e48a86b467d4a2c459"), "name" : "linda", "age" : 16, "gender" : "female", "grade" : 96 }

6.按照学生的分数进行排序
从小到大
> db.students.find().sort({grade:1})
从大到小,倒序
> db.students.find().sort({grade:-1})

7.将所有女生年龄增加一岁
> db.students.update({gender:"female"}, {$inc:{age:1}}, {multi:true})
WriteResult({ "nMatched" : 9, "nUpserted" : 0, "nModified" : 9 })