1. 程式人生 > 實用技巧 >MongoDB與python的互動

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()