username = 'davelee'
password = 'korea123'
connection = pymongo.MongoClient('mongodb://%s:%s@www.funcoding.xyz' % (username, password))
db = connection.test_db
# pipeline 을 리스트로 만들고, dictionary 타입으로 append 해서 파이프라인의 가독성을 높임
pipelines = list()
pipelines.append({'$group' : {'_id' : '$state', 'total_pop' : {'$sum' : '$pop'}}})
pipelines.append({'$match' : {'total_pop' : {'$gte' : 10 * 1000 * 1000}}})
results = db.zip.aggregate(pipelines)
for doc in results:
# 필요한 데이터만 추출 가능
print(doc['_id'], doc['total_pop'])
username = ''
password = ''
connection = pymongo.MongoClient('mongodb://%s:%s@www.funcoding.xyz' % (username, password))
db = connection.test_db
pipelines = list()
pipelines.append({ '$group': { '_id': { 'state': "$state", 'city': "$city" }, 'pop': { '$sum': "$pop" }}})
pipelines.append({ '$group': { '_id': "$_id.state", 'avg_city_pop': { '$avg': "$pop" }}})
results = db.zip.aggregate(pipelines)
for doc in results:
print(doc)
community edition 설치
terminal에서 아래의 명령어 차례대로 수행
sudo apt-get install -y mongodb-org
sudo vi /etc/mongod.conf
sudo service mongod start or restart
Mongodb CRUD (deprecated)
insert
update
find / findOne