Python操作mongodb,通过pymongo进行操作。
首先通过pip安装pymongo:
pip install pymongo
其次就是pymongo的一些简单操作了,如下:
# coding: utf-8 # In[ ]: from pymongo import MongoClient import datetime '''Create Mongodb Connection''' #client = MongoClient() #Default Connection client = MongoClient('localhost', 27017) #client = MongoClient('mongodb://localhost:27017/') # In[ ]: '''Getting a Database''' db = client.pyctp #db = client['pyctp'] #get db with dict usage # In[ ]: '''Getting a Collection''' coll = db.coll_pyctp #collection = db['coll_pyctp'] #get collection with dict usage # In[ ]: '''CREATE__Inserting a Document''' order = {"BrokerID":9999, "InvestorID":"058176", "InstrumentID":"cu1609"} order_many = [{"BrokerID":"9999", "InvestorID":"058176", "InstrumentID":"cu1609"}, {"BrokerID":"9999", "InvestorID":"123456", "InstrumentID":"cu1608"}] coll.insert_one(order) coll.insert_many(order_many) # In[ ]: '''RESEARCH__Getting Documents''' coll.find_one() for item in coll.find({}): print(item) coll.find_one({"InvestorID":"058176"}) # In[ ]: '''UPDATE''' coll.update_one({"InvestorID":"058176"},{"$set":{"Email":"jy_ypf@126.com","Password":"123456"}}) # In[ ]: '''DELETE''' #coll.delete_many({"InvestorID":"123456"}) coll.delete_one({"InvestorID":"058176"}) for item in coll.find({}): print(item) coll.find({}).count()
文章的脚注信息由WordPress的wp-posturl插件自动生成