-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyMongoHello.py
More file actions
executable file
·113 lines (74 loc) · 2.34 KB
/
PyMongoHello.py
File metadata and controls
executable file
·113 lines (74 loc) · 2.34 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
# encoding: utf-8
import sys
import pymongo
from pymongo import Connection
def ConnectMongo():
print 'Connecting to Mongo'
try:
connection = Connection() #connect default host and port
#connection = Connection('localhost', 27017) #connect specific host and port
except:
print 'Connection failed!'
return connection
def GetDatabase(connection, sDbName):
#db = connection.test_database
db = connection[sDbName]
return db
def GetCollection(db, sCollectionName):
#collection = db.test_collection
collection = db[sCollectionName]
return collection
def InsertDocument(db, sCollectionName):
print '\nInserting document'
db[sCollectionName].insert({'_id': 42, 'name': 'My Document', 'ids': [1,2,3], 'subdocument': {'a':2}})
pass
def BatchInsert(db, sCollectionName):
db[sCollectionName].insert([{'a':1}, {'a':2}])
pass
def ListCollection(db, sCollectionName):
print '\nListing - ' , sCollectionName, ' :'
lColl = list(db[sCollectionName].find())
for item in lColl:
print item
pass
def FindDocument(db, sCollectionName, sFind):
print '\nFind ' , sFind, ' in ', sCollectionName
#find returns a python iterator
for str in db[sCollectionName].find({'name' : 'Aurora'}):
print str
pass
def RemoveData(db, sCollectionName):
print '\nDeleting data'
db[sCollectionName].remove({'a': {'$gt': 1}})
pass
def UpdateData(db, sCollectionName):
print '\nUpdating data'
#it is also possible to reach a's sub attributes using 'a.b':1 etc
db[sCollectionName].update({'a': 1}, {'$set': {'a': 7}})
pass
def main():
print 'it is started'
#sDbName = 'tentips'
sDbName = 'test'
sCollectionName = 'unicorns'
connection = ConnectMongo()
db = GetDatabase(connection, sDbName)
collection = GetCollection(db, sCollectionName)
print '\nPrinting collection count:'
print collection.count()
print '\nPrinting first item in coll:'
print collection.find_one()
print '\nPrinting collection:'
print collection.find()
#InsertDocument(db, sCollectionName)
#BatchInsert(db, sCollectionName)\
ListCollection(db, sCollectionName)
FindDocument(db, sCollectionName, 'hakan')
#RemoveData(db, sCollectionName)
ListCollection(db, sCollectionName)
UpdateData(db, sCollectionName)
ListCollection(db, sCollectionName)
sys.exit()
if __name__ == '__main__':
main()