Skip to content

Commit be3ef42

Browse files
committed
wip
1 parent 3ec3fb3 commit be3ef42

File tree

17 files changed

+1328
-0
lines changed

17 files changed

+1328
-0
lines changed

python/mongodb-mini-book/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.venv/
2+
.vscode/
3+
.idea/
4+
__pypackages__/
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
*log
89.3 KB
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
use school
3+
db.scores.drop();
4+
var types = ['exam', 'homework', 'quiz']
5+
for (student_id = 0; student_id < 100; student_id++) {
6+
for (type=0; type < 3; type++) {
7+
var r = {'student_id':student_id, 'type':types[type], 'score':Math.random() * 100};
8+
db.scores.insert(r);
9+
}
10+
}
11+
12+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
import pymongo
3+
4+
# It is not necessary to import sys
5+
6+
# establish a connection to the database
7+
connection = pymongo.MongoClient("mongodb://localhost")
8+
9+
# get a handle to the school database
10+
db = connection.school
11+
scores = db.scores
12+
13+
14+
def find():
15+
16+
print "find(), reporting for duty"
17+
18+
query = {'type': 'exam'}
19+
20+
try:
21+
cursor = scores.find(query)
22+
23+
except Exception as e:
24+
print "Unexpected error:", type(e), e
25+
26+
sanity = 0
27+
for doc in cursor:
28+
print doc
29+
sanity += 1
30+
if (sanity > 10):
31+
break
32+
33+
34+
def find_one():
35+
36+
print "find_one(), reporting for duty"
37+
query = {'student_id': 10}
38+
39+
try:
40+
doc = scores.find_one(query)
41+
42+
except Exception as e:
43+
print "Unexpected error:", type(e), e
44+
45+
print doc
46+
47+
48+
if __name__ == '__main__':
49+
find() # Change this to find_one() to run that function, instead.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
import pymongo
3+
4+
# establish a connection to the database
5+
connection = pymongo.MongoClient("mongodb://localhost")
6+
7+
# get a handle to the school database
8+
db = connection.school
9+
scores = db.scores
10+
11+
12+
def find():
13+
14+
print "find, reporting for duty"
15+
16+
query = {'type': 'exam', 'score': {'$gt': 50, '$lt': 70}}
17+
18+
try:
19+
cursor = scores.find(query)
20+
21+
except Exception as e:
22+
print "Unexpected error:", type(e), e
23+
24+
sanity = 0
25+
for doc in cursor:
26+
print doc
27+
sanity += 1
28+
if (sanity > 10):
29+
break
30+
31+
32+
if __name__ == '__main__':
33+
find()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
import json
4+
import urllib2
5+
import pymongo
6+
7+
# connect to mongo
8+
connection = pymongo.MongoClient("mongodb://localhost")
9+
10+
# get a handle to the reddit database
11+
db = connection.reddit
12+
stories = db.stories
13+
14+
# drop existing collection
15+
stories.drop()
16+
17+
# get the reddit home page
18+
reddit_page = urllib2.urlopen("http://www.reddit.com/r/technology/.json")
19+
20+
# parse the json into python objects
21+
parsed = json.loads(reddit_page.read())
22+
23+
# iterate through every news item on the page
24+
for item in parsed['data']['children']:
25+
# put it in mongo
26+
stories.insert_one(item['data'])

python/mongodb-mini-book/03-InsertPegandoDadosDoReddit/reddit.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
import pymongo
3+
4+
# no need to import sys
5+
6+
# establish a connection to the database
7+
connection = pymongo.MongoClient("mongodb://localhost")
8+
9+
# get a handle to the school database
10+
db = connection.school
11+
scores = db.scores
12+
13+
14+
def find():
15+
print "find, reporting for duty"
16+
query = {}
17+
try:
18+
cursor = scores.find(query).skip(4)
19+
cursor.limit(1)
20+
# cursor.sort('student_id', pymongo.ASCENDING).skip(4).limit(1)
21+
cursor.sort([('student_id', pymongo.ASCENDING),
22+
('score', pymongo.DESCENDING)])
23+
24+
except Exception as e:
25+
print "Unexpected error:", type(e), e
26+
27+
for doc in cursor:
28+
print doc
29+
30+
31+
if __name__ == '__main__':
32+
find()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
import pymongo
3+
4+
# establish a connection to the database
5+
connection = pymongo.MongoClient("mongodb://localhost")
6+
7+
8+
def insert_two():
9+
10+
# get a handle to the school database
11+
db = connection.school
12+
people = db.people
13+
14+
print "insert, reporting for duty"
15+
16+
richard = {"name": "Richard Kreuter", "company": "MongoDB",
17+
"interests": ['horses', 'skydiving', 'fencing']}
18+
andrew = {"_id": "erlichson", "name": "Andrew Erlichson",
19+
"company": "MongoDB", "interests": ['running', 'cycling',
20+
'photography']}
21+
22+
try:
23+
people.insert_one(richard)
24+
people.insert_one(andrew)
25+
26+
except Exception as e:
27+
print "Unexpected error:", type(e), e
28+
29+
print(richard)
30+
print(andrew)
31+
32+
33+
if __name__ == '__main__':
34+
insert_two()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
3+
import pymongo
4+
5+
# establish a connection to the database
6+
connection = pymongo.MongoClient("mongodb://localhost")
7+
8+
9+
def insert_many():
10+
# get a handle to the school database
11+
db = connection.school
12+
people = db.people
13+
14+
print "insert_many, reporting for duty"
15+
andrew = {"_id": "erlichson", "name": "Andrew Erlichson",
16+
"company": "MongoDB",
17+
"interests": ['running', 'cycling', 'photography']}
18+
richard = {"name": "Richard Kreuter", "company": "MongoDB",
19+
"interests": ['horses', 'skydiving', 'fencing']}
20+
people_to_insert = [andrew, richard]
21+
try:
22+
people.insert_many(people_to_insert, ordered=False)
23+
except Exception as e:
24+
print "Unexpected error:", type(e), e
25+
26+
27+
def print_people():
28+
# get a handle to the school database
29+
db = connection.school
30+
people = db.people
31+
cur = people.find({}, {'name': 1})
32+
for doc in cur:
33+
print doc
34+
35+
if __name__ == '__main__':
36+
print "Before the insert_many, here are the people"
37+
print_people()
38+
insert_many()
39+
print "\n\nAfter the insert_many, here are the people"
40+
print_people()

0 commit comments

Comments
 (0)