Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b3de902

Browse files
write multiple docs
1 parent 5fdd832 commit b3de902

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

‎README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,58 @@ Making a connection with authentication:
4646
>>> db.collection_names()
4747
[u'flask_reminders', u'test', u'usersessions', u'messages']
4848
```
49+
50+
### Write One Document
51+
52+
Create a database `store_db`, use the `transactions` collection and write a document to it.
53+
54+
```
55+
>>> db = client.store_db
56+
>>> transactions = db.transactions
57+
>>> doc_data = {
58+
'store_name': 'sportsmans',
59+
'branch_name': 'tygervalley',
60+
'account_id': 'sns_03821023',
61+
'total_costs': 109.20,
62+
'products_purchased': ['cricket bat', 'cricket ball', 'sports hat'],
63+
'purchase_method':
64+
'credit card'
65+
}
66+
>>> response = transactions.insert_one(doc_data)
67+
>>> response.inserted_id
68+
ObjectId('5cad16a5a5f3826f6f046d74')
69+
```
70+
71+
We can verify that the collection is present:
72+
73+
```
74+
>>> db.collection_names()
75+
[u'transactions']
76+
```
77+
78+
### Write Many Documents
79+
80+
We can batch up our writes:
81+
82+
```
83+
>>> transaction_1 = {
84+
'store_name': 'sportsmans', 'branch_name': 'tygervalley',
85+
'account_id': 'sns_09121024', 'total_costs': 129.84,
86+
'products_purchased': ['sportsdrink', 'sunglasses', 'sports illustrated'],
87+
'purchase_method': 'credit card'
88+
}
89+
>>> transaction_2 = {
90+
'store_name': 'burger king', 'branch_name':
91+
'somerset west', 'account_id': 'bk_29151823',
92+
'total_costs': 89.99, 'products_purchased': ['cheese burger', 'pepsi'],
93+
'purchase_method': 'cash'
94+
}
95+
>>> transaction_3 = {
96+
'store_name': 'game', 'branch_name': 'bellvile', 'account_id': 'gm_49121229',
97+
'total_costs': 499.99, 'products_purchased': ['ps4 remote'],
98+
'purchase_method': 'cash'
99+
}
100+
>>> response = transactions.insert_many([transaction_1, transaction_2, transaction_3])
101+
>>> response.inserted_ids
102+
[ObjectId('5cad18d4a5f3826f6f046d75'), ObjectId('5cad18d4a5f3826f6f046d76'), ObjectId('5cad18d4a5f3826f6f046d77')]
103+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /