forked from deftlabs/mongodb-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo_example.py
More file actions
101 lines (81 loc) · 2.76 KB
/
Copy pathmongo_example.py
File metadata and controls
101 lines (81 loc) · 2.76 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
#
# Copyright 2011, Deft Labs.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# A simple example of how to use PyMongo. For more information, take
# a look at the http://api.mongodb.org/python/ documentation.
#
import datetime
import hashlib
#
# Make sure pymongo is installed
#
try:
import pymongo
except:
sys.exit('ERROR - pymongo not installed - see: http://api.mongodb.org/python/ - run: easy_install pymongo')
#
# Verify the version of pymongo is kosher
#
pyv = pymongo.version
pyv = pyv.partition( "+" )[0]
if map(int, pyv.split('.')) < [1,7]:
sys.exit('ERROR - this example requires pymongo 1.7 or higher: easy_install -U pymongo')
from pymongo import ASCENDING, DESCENDING
#
# Get access to the pythonExample collection in the test database
#
mongo = pymongo.Connection('localhost', 27017)
testDb = mongo.test
#
# Drop the pythonExample collection
#
testDb.drop_collection('pythonExample')
col = testDb.pythonExample
#
# Create an index on the name field
#
col.create_index([("name", DESCENDING)])
#
# Create the buoy object
#
buoy = {
'_id' : hashlib.md5('Station 44025 - LLNR - 830').hexdigest(),
'name' : 'Station 44025 - LLNR - 830',
"lastUpdate": datetime.datetime.utcnow()
}
#
# Insert the buoy into the collection
#
col.insert(buoy)
#
# Lookup the buoy
#
buoy = col.find_one({ '_id' : hashlib.md5('Station 44025 - LLNR - 830').hexdigest() });
print('Found buoy: ' + str(buoy))
#
# Add a buoy using upsert
#
buoyData = { 'readingType' : 'wind', 'value' : '10' }
col.update({"_id" : hashlib.md5('Station 44026 - LLNR - 831').hexdigest()}, { '$set' : { 'name' : 'Station 44026 - LLNR - 831', 'lastUpdate' : datetime.datetime.utcnow() }, '$addToSet' : { 'data' : buoyData } }, True);
#
# Add the same data... only the lastUpdate field should change.
#
col.update({"_id" : hashlib.md5('Station 44026 - LLNR - 831').hexdigest()}, { '$set' : { 'name' : 'Station 44026 - LLNR - 831', 'lastUpdate' : datetime.datetime.utcnow() }, '$addToSet' : { 'data' : buoyData } }, True);
#
# Add some new data.
#
buoyData = { 'readingType' : 'wind', 'value' : '20' }
col.update({"_id" : hashlib.md5('Station 44026 - LLNR - 831').hexdigest()}, { '$set' : { 'name' : 'Station 44026 - LLNR - 831', 'lastUpdate' : datetime.datetime.utcnow() }, '$addToSet' : { 'data' : buoyData } }, True);