python之MongoDB常用操作

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

本文主要列举pythonMongoDB的常用操作

前提准备

本文默认你已经成功安装mangodb且已开启该服务。以下为我们准备的数据表内容,表名为study

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* 1 createdAt:2019/10/9 下午1:43:34*/
{
"_id" : ObjectId("5d9d738695d02910481b4c37"),
"name" : "张妮",
"age" : 17
},

/* 2 createdAt:2019/10/9 下午1:43:34*/
{
"_id" : ObjectId("5d9d738695d02910481b4c36"),
"name" : "范泽东",
"age" : 24
},

/* 3 createdAt:2019/10/9 下午1:41:42*/
{
"_id" : ObjectId("5d9d7316d9cb8d5249552eb9"),
"name" : "范泽熙",
"age" : 23
},

/* 4 createdAt:2019/10/9 上午11:27:58*/
{
"_id" : ObjectId("5d9d53be8b8c5375d79fc163"),
"name" : "haha",
"age" : 99
},

/* 5 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c976d"),
"name" : "光绪",
"age" : 95
},

/* 6 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c976c"),
"name" : "朱重八",
"age" : 205
},

/* 7 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c976b"),
"name" : "李世民",
"age" : 100
},

/* 8 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c976a"),
"name" : "哪吒",
"age" : 246
},

/* 9 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c9769"),
"name" : "沙悟净",
"age" : 238
},

/* 10 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c9768"),
"name" : "唐僧",
"age" : 244
},

/* 11 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c9767"),
"name" : "猪八戒",
"age" : 489
},

/* 12 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c9766"),
"name" : "孙悟空",
"age" : 489
},

/* 13 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c9765"),
"name" : "黎明",
"age" : 45
},

/* 14 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c9764"),
"name" : "郭富城",
"age" : 48
},

/* 15 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c9763"),
"name" : "刘德华",
"age" : 50,
"hobby" : [
"唱歌",
"拍电影",
"吃饭",
"睡觉",
"耍帅",
"捐钱"
]
},

/* 16 createdAt:2019/10/9 上午11:21:03*/
{
"_id" : ObjectId("5d9d521fef094910187c9762"),
"name" : "张学友",
"age" : 45,
"hobby" : [
"唱歌",
"拍电影",
"捐钱"
]
},

/* 17 createdAt:2019/10/9 上午11:16:55*/
{
"_id" : ObjectId("5d9d51278b8c5375d79fc162"),
"name" : "tiny",
"age" : 17
},

/* 18 createdAt:2019/10/9 上午11:15:51*/
{
"_id" : ObjectId("5d9d50e78b8c5375d79fc161"),
"name" : "tony",
"age" : 20
}

安装pymongo

要在python中使用mongodb需要先安装该库

1
pip install pymongo

连接数据库

1
2
3
4
5
6
7
# 引入mongo库
import pymongo
from bson import ObjectId
# 连接Mongo
conn = pymongo.MongoClient(host='localhost', port=27017)
# 选择数据库
db = conn['local']

查询

python中操作mango代码是非常简单的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 查询所有
res = db.study.find({})
# 查询所有,只显示name
res = db.study.find({}, {"_id":0, "name":1})
# 从第3条开始查询5条
res = db.study.find().skip(2).limit(5)
# 按年龄降序
res = db.study.find().sort("age", pymongo.DESCENDING)
res = db.study.find().sort("age", -1)
# 按年龄升
res = db.study.find().sort("age", pymongo.ASCENDING)
res = db.study.find().sort("age", 1)
# 查询姓名为张学友的
res = db.study.find({"name":'张学友'})
# 查询年龄大于100的
res = db.study.find({"age":{'$gt':100}})
# 查询年龄小于100的
res = db.study.find({"age":{'$lt':100}})
# 查询年龄大于等于100的
res = db.study.find({"age":{'$gte':100}})
# 查询年龄小于等于100的
res = db.study.find({"age":{'$lte':100}})
# 查询_id=5d9d521fef094910187c976b的数据
res = db.study.find({"_id":ObjectId("5d9d521fef094910187c976b")})
# 查询年龄为17,45,50的数据
res = db.study.find({"age":{'$in':(17, 45, 50)}})
# 查询年龄为17或姓名等于黎明的
res = db.study.find({"$or":[{"age":17}, {"name":"黎明"}]})
# 查询爱好包含唱歌、拍电影睡觉的数据
res = db.study.find({"hobby":{"$all":["唱歌", '拍电影', "捐钱"]}})
print(list(res))

新增

1
2
3
4
5
6
# 增加一条
res = db.study.insert_one({"name":'ddd', "age":999})
print(res, type(res), res.inserted_id)
# 增加多条
res = db.study.insert_many([{"name":'范泽东', "age":23}, {"name":'张妮', "age":22}])
print(res, type(res), res.inserted_ids)

修改

修改来说操作相对多一点,牵涉到每条记录的属性修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 修改单条
res = db.study.update_one({"name":"张妮"}, {"$set":{"age":16}})
print(res, dir(res), res.raw_result, res.upserted_id)
# 修改多条
res = db.study.update_many({"age":{"$gt":400}}, {"$set":{"age":499}})
print(res, dir(res), res.raw_result, res.upserted_id)
# 年龄小于30岁的增加1岁
res = db.study.update_many({"age":{"$lt":30}}, {"$inc":{"age":1}})
print(res, dir(res), res.raw_result, res.upserted_id)
# 年龄大于100的减去10岁
res = db.study.update_many({"age":{"$gt":100}}, {"$inc":{"age":-10}})
print(res, dir(res), res.raw_result, res.upserted_id)

# 使用$set,有该属性则修改,没有则添加
# 为张学友设置爱好属性
res = db.study.update_one({"name":"张学友"}, {"$set":{"hobby":'唱歌'}})
# 为刘德华设置多个爱好
res = db.study.update_one({"name":"刘德华"}, {"$set":{"hobby":["唱歌", "拍电影", "吃饭", "睡觉", "耍帅", "捐钱"]}})
print(res, dir(res), res.raw_result, res.upserted_id)

# 使用$push为已有属性多添加一项内容
# 为刘德华多添加一个爱好
res = db.study.update_one({"name":"刘德华"}, {"$push":{"hobby":"耍帅"}})
print(res, dir(res), res.raw_result, res.upserted_id)

# 使用$pull删除已有属性中的某项内容
# 删除一个爱好
res = db.study.update_one({"name":"刘德华"}, {"$pull":{"hobby":"吃饭"}})
print(res, dir(res), res.raw_result, res.upserted_id)

# 使用$pop删除已有属性中首个或最后一项内容
# 删除第一个爱好
res = db.study.update_many({"name":"刘德华"}, {"$pop":{"hobby":-1}})
print(res, dir(res), res.raw_result, res.upserted_id)
# 删除最后一个爱好
res = db.study.update_many({"name":"刘德华"}, {"$pop":{"hobby":1}})
print(res, dir(res), res.raw_result, res.upserted_id)

删除

1
2
3
4
5
6
# 删除单条
res = db.study.delete_one({"age":998})
print(res, dir(res), res.deleted_count, res.raw_result)
# 删除多条
res = db.study.delete_many({"age":999})
print(res, dir(res), res.deleted_count, res.raw_result)