/
pymongo_shell.py
39 lines (32 loc) · 1.05 KB
/
pymongo_shell.py
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
"""
Convenience functions for MongoDB in the Python shell
@Author: Joe.Drumgoole@mongodb.com
"""
import pprint
import pymongo
def get_collection(host, db_name, collection_name ):
client = pymongo.MongoClient(host=host)
db = client[db_name]
collection = db[collection_name]
return collection
def print_cursor(cursor, plain_print=True, buffer_size=20):
try:
buffer_count = 0
for doc in cursor:
if len(str(doc)) < 80 or plain_print:
print(doc)
buffer_count += 1
if buffer_count == buffer_size:
print("Hit Return to continue")
_ = input()
buffer_count = 0
else:
for line in pprint.pformat(doc).splitlines():
print(line)
buffer_count += 1
if buffer_count == buffer_size:
print("Hit Return to continue")
_ = input()
buffer_count = 0
except KeyboardInterrupt:
return