Python 连接MongoDB数据库
-
1 PyMango 安装
要想在Python中使用MongoDB,需要安装PyMongo,通过PyMongo 我们在Python中就可以链接MongoDB数据库了,这里我是使用了Pip来安装的,
pip install pymongo
完成了这条指令之后,我们就可以在Python当中进行数据库的连接操作了。
2 Python链接代码在这里我直接给出一个示例了,注意的是,Pymango可以直接把Python中dict类型的数据直接存入,也可以直接取出,是不是很方便?
#encoding:utf=8
import pymongo as pm
import jsonclass MongoOperator:
def __init__(self, host, port, db_name, default_collection):
'''
设置mongodb的地址,端口以及默认访问的集合,后续访问中如果不指定collection,则访问这个默认的
:param host: 地址
:param port: 端口
:param db_name: 数据库名字
:param default_collection: 默认的集合
'''
#建立数据库连接
self.client = pm.MongoClient(host=host, port=port)
#选择相应的数据库名称
self.db = self.client.get_database(db_name)
#设置默认的集合
self.collection = self.db.get_collection(default_collection)def insert(self, item, collection_name =None):
'''
插入数据,这里的数据可以是一个,也可以是多个
:param item: 需要插入的数据
:param collection_name: 可选,需要访问哪个集合
:return:
'''
if collection_name != None:
collection = self.db.get_collection(self.db)
collection.insert(item)
else:
self.collection.insert(item)def find(self, expression =None, collection_name=None):
'''
进行简单查询,可以指定条件和集合
:param expression: 查询条件,可以为空
:param collection_name: 集合名称
:return: 所有结果
'''
if collection_name != None:
collection = self.db.get_collection(self.db)
if expression == None:
return collection.find()
else:
return collection.find(expression)
else:
if expression == None:
return self.collection.find()
else:
return self.collection.find(expression)def get_collection(self, collection_name=None):
'''
很多时候单纯的查询不能够通过这个类封装的方法执行,这时候就可以直接获取到对应的collection进行操作
:param collection_name: 集合名称
:return: collection
'''
if collection_name == None:
return self.collection
else:
return self.get_collection(collection_name)db = MongoOperator('10.0.0.39',27017,'test_db','test_collection')
item = {}
item['name'] = 'mebiuw'
item['age'] = '23'
db.insert(item)
for item in db.find():
print(item)
-
怎麼使用 GridFS ?