Skip to content

Commit 9d00658

Browse files
authored
Update README.md
1 parent f04ca60 commit 9d00658

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,51 @@ MongoDB also has a count method:
131131
>>> transactions.find({'purchase_method': 'cash'}).count()
132132
2
133133
```
134+
135+
### Filters
136+
137+
Find all the documents with purchase price > 120:
138+
139+
```
140+
>>> response = transactions.find({'total_costs': {'$gt': 120}})
141+
>>> [doc for doc in response]
142+
[{u'account_id': u'sns_09121024', u'store_name': u'sportsmans', u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'products_purchased': [u'sportsdrink', u'sunglasses', u'sports illustrated'], u'_id': ObjectId('5cad18d4a5f3826f6f046d75'), u'total_costs': 129.84}, {u'account_id': u'gm_49121229', u'store_name': u'game', u'purchase_method': u'cash', u'branch_name': u'bellvile', u'products_purchased': [u'ps4 remote'], u'_id': ObjectId('5cad18d4a5f3826f6f046d77'), u'total_costs': 499.99}]
143+
```
144+
145+
### Projections
146+
147+
Select specific fields from the returned response:
148+
149+
```
150+
>>> response = transactions.find({}, {'branch_name': 'tygervalley', 'purchase_method': 'credit card'})
151+
>>> [doc for doc in response]
152+
[{u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'_id': ObjectId('5cad16a5a5f3826f6f046d74')}, {u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'_id': ObjectId('5cad18d4a5f3826f6f046d75')}, {u'purchase_method': u'cash', u'branch_name': u'somerset west', u'_id': ObjectId('5cad18d4a5f3826f6f046d76')}, {u'purchase_method': u'cash', u'branch_name': u'bellvile', u'_id': ObjectId('5cad18d4a5f3826f6f046d77')}]
153+
```
154+
155+
### Sorting Documents
156+
157+
Sorting Documents in Descending Order:
158+
159+
```
160+
>>> from pymongo import MongoClient, DESCENDING
161+
>>> response = transactions.find().sort('total_costs', DESCENDING)
162+
>>> ['Products: {}, Price: {}'.format(doc['products_purchased'], doc['total_costs']) for doc in response]
163+
["Products: [u'ps4 remote'], Price: 499.99", "Products: [u'sportsdrink', u'sunglasses', u'sports illustrated'], Price: 129.84", "Products: [u'cricket bat', u'cricket ball', u'sports hat'], Price: 109.2", "Products: [u'cheese burger', u'pepsi'], Price: 89.99"]
164+
```
165+
166+
### Aggregations
167+
168+
```
169+
>>> agr = [{'$group': {'_id': 1, 'all': { '$sum': '$total_costs' }}}]
170+
>> [a for a in transactions.aggregate(agr)]
171+
[{u'all': 829.02, u'_id': 1}]
172+
```
173+
174+
or:
175+
176+
```
177+
>>> agr = [{'$group': {'_id': 1, 'all': { '$sum': '$total_costs' }}}]
178+
>>> val = list(transactions.aggregate(agr))
179+
>>> val
180+
[{u'all': 829.02, u'_id': 1}]
181+
```

0 commit comments

Comments
 (0)