File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+
File renamed without changes.
You can’t perform that action at this time.
0 commit comments