$ pip install pymongo
>>> import pymongo
>>> from pymongo import MongoClient >>> client = MongoClient('127.0.0.1', 27017)
>>> client = MongoClient('mongodb://127.0.0.1:27017') >>> print client MongoClient(host=['127.0.0.1:27017'], document_class=dict, tz_aware=False, connect=True)
>>> db = client.addressBook >>> db = client['addressBook']
>>> collection = db.office >>> collection = db['office']
>>> import datetime >>> post = {"author": "Mike", ... "text": "My first blog post!", ... "tags": ["mongodb", "python", "pymongo"], ... "date": datetime.datetime.utcnow()}
import pymongo import json import datetime from pymongo import MongoClient client = MongoClient("localhost", 27017) db = client.nlu db.logging mydata = """ {"name": "yundream", "age":35} """ d = json.loads(mydata) d['date'] = datetime.datetime.utcnow() postid = db.logging.insert_one(d).inserted_id
import pymongo import pprint from pymongo import MongoClient client = MongoClient("localhost", 27017) db = client.nlu pprint.pprint (db.logging.find_one())
{u'_id': ObjectId('595df7bb7870ae0e675e49c7'), u'age': 35, u'date': datetime.datetime(2017, 7, 6, 8, 41, 31, 851000), u'name': u'yundream'}
>>> pprint.pprint (db.logging.find_one({"name":"hani"})) {u'_id': ObjectId('595f76027870ae60e400d454'), u'age': 38, u'date': datetime.datetime(2017, 7, 7, 11, 52, 34, 309000), u'name': u'hani'}
>>> print (db.logging.find_one({"_id": '595df7bb7870ae0e675e49c7'}))
>>> from bson.objectid import ObjectId >>> pprint.pprint(db.logging.find_one({"_id": ObjectId('595df7bb7870ae0e675e49c7')})) {u'_id': ObjectId('595df7bb7870ae0e675e49c7'), u'age': 35, u'date': datetime.datetime(2017, 7, 6, 8, 41, 31, 851000), u'name': u'yundream'}
>>> for log in db.logging.find(): ... pprint.pprint(log) ... {u'_id': ObjectId('595df7bb7870ae0e675e49c7'), u'age': 35, u'date': datetime.datetime(2017, 7, 6, 8, 41, 31, 851000), u'name': u'yundream'} {u'_id': ObjectId('595f76027870ae60e400d454'), u'age': 38, u'date': datetime.datetime(2017, 7, 7, 11, 52, 34, 309000), u'name': u'hani'}
Copyrights © - Joinc, All Rights Reserved. Inherited From - Yundream Rebranded By - Joonphil
Contents
Pymongo
데이터베이스 및 컬랙션 접근
문서 저장
find_one() 으로 문서 하나 가져오기
Object_ID 로 가져오기
하나 이상의 문서는 find()
Recent Posts
Archive Posts
Tags