Pymongo 基本操作

这几天项目需要用MongoDB存储数据, 在此记录下需要用到的操作,以便日后查阅以及有相同需要的同学参考,如有误或改进欢迎提出:)

1. 安装 driver

MongoDB 的Python driver是依赖于 pymongo 库, 我们要先安装 pymongo

1
pip install pymongo

2. 创建连接

1
2
3
4
5
import pymongo
conn = pymongo.MongoClient('localhost', 27017) # 端口默认27017可以不填
db = conn.test ## test 是数据库名称,也可以 conn['test']
collection = db.co ## co 是聚集名称, 也可以 db['co']

3. CURD操作

3.1 Insert

3.1.1 Singe

1
2
3
4
5
6
7
8
9
10
11
import pymongo
conn = pymongo.MongoClient('localhost', 27017) # 端口默认27017可以不填
db = conn.test ## test 是数据库名称,也可以 conn['test']
collection = db.co ## co 是聚集名称, 也可以 db['co']
collection.insert_one(
{"item": "canvas",
"qty": 100,
"tags": ["cotton"],
"size": {"h": 28, "w": 35.5, "uom": "cm"}})

3.1.2 Multiple

1
2
3
4
5
6
7
8
9
10
11
12
13
collection.insert_many([
{"item": "journal",
"qty": 25,
"tags": ["blank", "red"],
"size": {"h": 14, "w": 21, "uom": "cm"}},
{"item": "mat",
"qty": 85,
"tags": ["gray"],
"size": {"h": 27.9, "w": 35.5, "uom": "cm"}},
{"item": "mousepad",
"qty": 25,
"tags": ["gel", "blue"],
"size": {"h": 19, "w": 22.85, "uom": "cm"}}])

3.2 Query

3.2.1 Query All

1
cursor = collection.find({}) #相当于SQL 的 SELECT * FROM collection

3.2.2 Query Specify

1
2
3
4
5
6
7
8
9
10
cursor = collection.find({"status": "D"}) # Equal SELECT * FROM collection WHERE status = "D"
cursor = collection.find({"status": {"$in": ["A", "D"]}}) # Equal SELECT * FROM collection WHERE status in ("A", "D")
# AND
cursor = collection.find({"status": "A", "qty": {"$lt": 30}}) # Equal SELECT * FROM collection WHERE status = "A" AND qty < 30
#OR
cursor = collection.find(
{"$or": [{"status": "A"}, {"qty": {"$lt": 30}}]}) # Equal SELECT * FROM collection WHERE status = "A" OR qty < 30

3.3 Update

3.3.1 Update Single

1
2
3
4
collection.update_one(
{"item": "paper"},
{"$set": {"size.uom": "cm", "status": "P"},
"$currentDate": {"lastModified": True}}) #没有会新建

3.3.2 Update Multiple

1
2
3
4
collection.update_many(
{"qty": {"$lt": 50}},
{"$set": {"size.uom": "in", "status": "P"},
"$currentDate": {"lastModified": True}})

3.3.3 Replace

1
2
3
4
5
6
collection.replace_one(
{"item": "paper"},
{"item": "paper",
"instock": [
{"warehouse": "A", "qty": 60},
{"warehouse": "B", "qty": 40}]})

3.4 Delete

3.4.1 Delete All

1
collection.delete_many({})

3.4.2 Delete Specify

1
2
3
collection.delete_many({"status": "A"}) #删除所有匹配
collection.delete_one({"status": "D"}) # 删除第一个匹配的

4. Bulk Write

有时候我们需要插入大量数据的时候,用insert_many 可能服务器会吃不消,这时候可以用bulk。

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
# 返回一个BulkOperationBuilder instance
### 有方法如下
# + execute(write_concern=None) 执行所有操作
# + find(selector, collation=None) 返回 BulkWriteOperation instance
# + insert(document) 插入一条数据
###
bulk = collection.initialize_unordered_bulk_op() # 有序的是initialize_ordered_bulk_op()
bulk.insert( { user: "abc123", status: "A", points: 0 } );
bulk.insert( { user: "ijk123", status: "A", points: 0 } );
bulk.insert( { user: "mop123", status: "P", points: 0 } );
# bulk.find() 返回一个 BulkWriteOperation instance
### 有如下方法
# + remove() 移除所有数据
# + remove_one() 移除一个
# + replace_one(replacement) 替换一个
# + update(update)
# + update_one(update)
# + upsert() 返回一个 BulkUpsertOperation instance
###
bulk.find( { status: "D" } ).remove();
bulk.find( { status: "P" } ).update( { '$set': { 'comment': "Pending" } } );
# bulk.find().upsert()返回一个BulkUpsertOperation instance
### 有如下方法
# + replace_one(replacement)
# + update(update)
# + update_one(update)
###
bulk.find( {'ID' : "test"} ).upsert().update_one({'$setOnInsert' : {
'ID' : "test",
'itemID' : "123",
'status' : 0,
'score' : 0,
}})
bulk.execute()
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
附录 Update Operators
Fields
Name Description
$inc Increments the value of the field by the specified amount.
$mul Multiplies the value of the field by the specified amount.
$rename Renames a field.
$setOnInsert Sets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
$set Sets the value of a field in a document.
$unset Removes the specified field from a document.
$min Only updates the field if the specified value is less than the existing field value.
$max Only updates the field if the specified value is greater than the existing field value.
$currentDate Sets the value of a field to current date, either as a Date or a Timestamp.
Array
Operators
Name Description
$ Acts as a placeholder to update the first element that matches the query condition in an update.
$addToSet Adds elements to an array only if they do not already exist in the set.
$pop Removes the first or last item of an array.
$pullAll Removes all matching values from an array.
$pull Removes all array elements that match a specified query.
$pushAll Deprecated. Adds several items to an array.
$push Adds an item to an array.
Modifiers
Name Description
$each Modifies the push and addToSet operators to append multiple items for array updates.
$slice Modifies the $push operator to limit the size of updated arrays.
$sort Modifies the $push operator to reorder documents stored in an array.
$position Modifies the $push operator to specify the position in the array to add elements.
Bitwise
Name Description
$bit Performs bitwise AND, OR, and XOR updates of integer values.
Isolation
Name Description
$isolated Modifies the behavior of a write operation to increase the isolation of the operation.

reference:

https://docs.mongodb.com/master/reference/

http://api.mongodb.com/python/current/api/pymongo/bulk.html?highlight=bulk#module-pymongo.bulk