哆啦A梦 50周年应援

MongoDB与Python的交互

管理员运行命令提示符
使用pip安装pymongo包(pymongo是Python中用来操作MongoDB的一个库)

注意:先要开启MongoDB服务

1、添加文档

 1 from pymongo import MongoClient
 2 
 3 # 连接服务器
 4 conn = MongoClient("localhost", 27017)
 5 
 6 # 连接数据库
 7 db = conn.mydb
 8 
 9 # 获取集合
10 collection = db.student
11 
12 # 添加文档
13 # collection.insert_one({"name": "西施", "age": 17, "gender": 0, "address": "长沙", "isDelete": 0})
14 # 一次性添加多个
15 collection.insert_many([{"name": "娜可露露", "age": 20, "gender": 0, "address": "东京", "isDelete": 0}, {"name": "不知火舞", "age": 19, "gender": 0, "address": "神户", "isDelete": 0}])
16 
17 # 断开
18 conn.close()

2、查询文档

 1 from pymongo import MongoClient
 2 
 3 # 连接服务器
 4 conn = MongoClient("localhost", 27017)
 5 
 6 # 连接数据库
 7 db = conn.mydb
 8 
 9 # 获取集合
10 collection = db.student
11 
12 # 查询文档 (查询年龄>18)
13 res = collection.find({"age": {"$gt": 18}})
14 
15 for row in res:
16     print(row)
17     print(type(row))  # 字典类型
18     
19 '''
20 # 查询所有文件
21 res = collection.find()
22 
23 for row in res:
24     print(row)
25     print(type(row)) # 字典类型
26 '''
27     
28 # 断开
29 conn.close()

 1 from pymongo import MongoClient
 2 
 3 # 用id查询引入
 4 from bson.objectid import ObjectId
 5 
 6 #排序方法2 需要的库
 7 import pymongo
 8 
 9 # 连接服务器
10 conn = MongoClient("localhost", 27017)
11 
12 # 连接数据库
13 db = conn.mydb
14 
15 # 获取集合
16 collection = db.student
17 
18 '''
19 # 查询文档 (查询年龄>18)
20 res = collection.find({"age": {"$gt": 18}})
21 
22 for row in res:
23     print(row)
24     print(type(row))
25 
26 # 查询所有文件
27 res = collection.find()
28 
29 for row in res:
30     print(row)
31     print(type(row)) # 字典类型
32 
33 # 统计查询
34 res = collection.find({"age": 18}).count()
35 print(res)  # 4
36 
37 
38 # 根据id查询
39 res = collection.find({"_id": ObjectId("5f311d120606ec823eda6fcf")})
40 print(res[0])   # {'_id': ObjectId('5f311d120606ec823eda6fcf'), 'name': '瑶', 'age': 16.0, 'gender': 0.0, 'address': '重庆', 'isDelete': 0.0}
41 
42 '''
43 # 排序
44 res = collection.find().sort("age")  # 升序
45 
46 # res = collection.find().sort("age", -1)  # 降序
47 
48 # 升序方法2
49 # collection.find().sort("age", pymongo.ASCENDING)
50 
51 # 降序方法2
52 # res = collection.find().sort("age", pymongo.DESCENDING)
53 
54 for row in res:
55     print(row)
56 
57 # 断开
58 conn.close()

 1 from pymongo import MongoClient
 2 
 3 # 用id查询引入
 4 from bson.objectid import ObjectId
 5 
 6 #排序方法2 需要的库
 7 import pymongo
 8 
 9 # 连接服务器
10 conn = MongoClient("localhost", 27017)
11 
12 # 连接数据库
13 db = conn.mydb
14 
15 # 获取集合
16 collection = db.student
17 
18 '''
19 # 查询文档 (查询年龄>18)
20 res = collection.find({"age": {"$gt": 18}})
21 
22 for row in res:
23     print(row)
24     print(type(row))
25 
26 # 查询所有文件
27 res = collection.find()
28 
29 for row in res:
30     print(row)
31     print(type(row)) # 字典类型
32 
33 # 统计查询
34 res = collection.find({"age": 18}).count()
35 print(res)  # 4
36 
37 
38 # 根据id查询
39 res = collection.find({"_id": ObjectId("5f311d120606ec823eda6fcf")})
40 print(res[0])   # {'_id': ObjectId('5f311d120606ec823eda6fcf'), 'name': '瑶', 'age': 16.0, 'gender': 0.0, 'address': '重庆', 'isDelete': 0.0}
41 
42 '''
43 
44 '''
45 # 排序
46 res = collection.find().sort("age")  # 升序
47 
48 # res = collection.find().sort("age", -1)  # 降序
49 
50 # 升序方法2
51 # collection.find().sort("age", pymongo.ASCENDING)
52 
53 # 降序方法2
54 # res = collection.find().sort("age", pymongo.DESCENDING)
55 for row in res:
56     print(row)
57     
58 '''
59 # 分页查询 (越过3页.再查4页)
60 res = collection.find().skip(3).limit(4)
61 for row in res:
62     print(row)
63 
64 # 断开
65 conn.close()

3、更新文档

 1 from pymongo import MongoClient
 2 
 3 # 连接服务器
 4 conn = MongoClient("localhost", 27017)
 5 
 6 # 连接数据库
 7 db = conn.mydb
 8 
 9 # 获取集合
10 collection = db.student
11 
12 # 更新1条文档 (条件name:娜可露露, 修改age:15)
13 collection.update_one({"name": "娜可露露"}, {"$set": {"age": 15}})
14 # 用update_many()一次更新多个值
15 
16 # 断开
17 conn.close()

4、删除文档

 1 from pymongo import MongoClient
 2 
 3 # 连接服务器
 4 conn = MongoClient("localhost", 27017)
 5 
 6 # 连接数据库
 7 db = conn.mydb
 8 
 9 # 获取集合
10 collection = db.student
11 
12 # 删除1条文档 (条件name:不知火舞)
13 collection.delete_one({"name": "不知火舞"})
14 collection.delete_one({"name": "甄姬"})
15 
16 # 一次删除多条(条件为name:娜可露露)
17 # collection.remove({"name": "娜可露露"})
18 # collection.delete_many({"name": "娜可露露"})
19 
20 # 全部所有删除 慎用!
21 # collection.remove({})
22 # collection.delete_many({})
23 
24 # 断开
25 conn.close()

 

posted @ 2020-09-21 17:10  秋泊ソース  阅读(308)  评论(0编辑  收藏  举报