Skip to content

Commit b11802b

Browse files
committed
Merge pull request Show-Me-the-Code#13 from effyhuihui/master
TASK 0003 added
2 parents 6878058 + b5d9bb2 commit b11802b

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

effy/0003/0003_pymongo.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env
2+
'''
3+
Instead of Redis, I use MongoDB instead -- since I am using it in
4+
a regular basis:)
5+
Make sure that you have a live mongo connection running on port 27017,
6+
which is the default port.
7+
'''
8+
import uuid
9+
from pymongo import MongoClient
10+
## start a client to connect python to mongo
11+
c = MongoClient()
12+
## to use a specific db
13+
## > use mydb
14+
db = c['mydb']
15+
## to create a collection instance to access collection mongoPyPipe
16+
## I've already created this collection in mongo shell
17+
collection = db['mongoPyPipe']
18+
19+
## interatively insert doc to collection
20+
## > db.mongoPyPipe.insert({"id": i, "value": uuid})
21+
for i in range(200):
22+
collection.insert({"id": i, "value": uuid.uuid4()})
23+
24+
25+
26+
27+
28+

effy/0003/readme.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
python和mongo的连接出乎意料的简单。
2+
需要的只是一个pymongo中的MongoClient来建立一个live connection!
3+
4+
而且最赞的是,由于是nosql,根本不需要考虑collection原来的数据,key,schema什么的,直接统统insert。
5+
6+
在之前和relational sql连接的时候(postgres)需要考虑原来的table里面的PK有没有重复,id要被maintai成城incremental,这种问题。
7+

0 commit comments

Comments
 (0)