python操作mongodb数据库①mongodb的安装和简单使用
参考文档:
中文版:http://www.mongoing.com/docs/crud.html
英文版:https://docs.mongodb.com/
环境准备:
1.下载mongodb for windows 2008r2版本,进行安装,建议安装在盘的根目录
比如安装在d:/mongodbserver目录下
安装完数据库以后可以设置环境变量,这样直接就可以在cmd命令行进行操作了
2.创建数据目录
D:\mongodbserver\data
3.cmd命令行下启动mongodb程序
D:\mongodbserver\bin>mongod --dbpath D:\mongodbserver\data
基本概念:
文档:区分大小写、key唯一不可重复、文档可嵌套、键值对是有序的
集合:集合就是一组文档,文档类似关系数据库里的行,集合类似于关系数据库里的表,集合中的文档无需固定的结构(与关系型数据库的区别)
集合的命名规范:
1.不能是空字符串("")
2.不能包含\0字符(空字符)
3.不能使用system.的前缀(系统保留)
4.建议不包含保留字"$"
5.用.分割不同命名空间的子集合(如:blog.users, blog.posts)
重点:
1.多个文档组成集合,多个集合组成数据库
2.一个实例可以承载多个数据库
3.每个数据库都有独立的权限
4.保留的数据库名称(admin,local,config)
基本操作:
cmd窗口敲入mongo回车进入命令行
1.函数的定义和使用
2.新增数据
> use students;
switched to db students
> stu = {"name":"jack","age": }
{ "name" : "jack", "age" : }
> db.students.insert(stu)
WriteResult({ "nInserted" : })
插入单条、多条数据
>db.students.insertMany([{"name":"lucy","age":},{"name":"lilei","age":},{"name":"hanmeimei","age":}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5a9d58fbcfb5edc5e31235f4"),
ObjectId("5a9d58fbcfb5edc5e31235f5"),
ObjectId("5a9d58fbcfb5edc5e31235f6")
]
}
> db.students.find()
{ "_id" : ObjectId("5a9d5822cfb5edc5e31235f0"), "name" : "jack", "age" : }
{ "_id" : ObjectId("5a9d5892cfb5edc5e31235f1"), "name" : "lily", "age" : }
{ "_id" : ObjectId("5a9d58a3cfb5edc5e31235f2"), "name" : "tom", "age" : }
{ "_id" : ObjectId("5a9d58cdcfb5edc5e31235f3"), "name" : "lucy", "age" : }
{ "_id" : ObjectId("5a9d58fbcfb5edc5e31235f4"), "name" : "lucy", "age" : }
{ "_id" : ObjectId("5a9d58fbcfb5edc5e31235f5"), "name" : "lilei", "age" : }
{ "_id" : ObjectId("5a9d58fbcfb5edc5e31235f6"), "name" : "hanmeimei", "age" : }
2.数据的查询
查询所有数据
3.查询单条数据
4.修改数据
5.删除数据
6.删除所有数据
> db.students.deleteMany({})
{ "acknowledged" : true, "deletedCount" : }
> db.students.find()
练习任务:
1.创建一个学生信息表(至少包含:姓名、性别、成绩、年龄)
2.写入15条不同的数据
db.students.insertMany(
[
{name: "bob", age:, gender:"male", grade:},
{name: "ana", age:, gender:"female", grade:},
{name: "xi", age:, gender:"male", grade:},
{name: "bob1", age:, gender:"male", grade:},
{name: "ana1", age:, gender:"female", grade:},
{name: "jack", age:, gender:"male", grade:},
{name: "tom", age:, gender:"male", grade:},
{name: "lily", age:, gender:"female", grade:},
{name: "lucy", age:, gender:"female", grade:},
{name: "lilei", age:, gender:"female", grade:},
{name: "hanmeimei", age:, gender:"female", grade:},
{name: "harry", age:, gender:"male", grade:},
{name: "json", age:, gender:"male", grade:},
{name: "jim", age:, gender:"male", grade:},
{name: "rose", age:, gender:"female", grade:},
{name: "moli", age:, gender:"female", grade:},
{name: "linda", age:, gender:"female", grade:}
]
)
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 })