monogodb基本操作,python语言描述

 

python 语言描述

文件结构描述

db--

      |

      config.py

      |

      mongodb_api.py

     | 

     mongodb_operatioin.py

     |

     test_mongodb.py

----|

config.py

# mongodb 配置文件

MONGODB_HOST = 'localhost'

MONGODB_PORT = 27017

MONGODB_USER = 'user'

MONGODB_PASSWORD = '123456'

MONGODB_DB = 'admin'

MONGODB_DB_TEST = 'testdb'

 

mongodb_api.py

from pyMongo import MongoClient

from db.mongodb_operation import BaseMG

from db.config import MONGODB_HOST,MONGODB_PORT, MONGODB_USER,MONGODB_PASSWORD

class MongodbAPI(object):

-------------------------------Mongodb 连接---------------------------

1. 连接MongoDB

1)无密码连接

def __init__(self):

conn = MongoClient(host=MOGODB_HOST, port=MOGODB_PORT)

db = conn.test # admin 这个数据库要用于登陆不验证,一般不用

self.collection = db.datasource # 没有datasource会自动创建

 

2)有密码登陆验证连接

def __init__(self):

conn = MongoClient(host=MONGODB_HOST, port=MONGODB_PORT)

db = conn.admin

db.authenticate(name=MONGODB_USER, password=MONGODB_PASSWORD)

testdb = conn.testdb

self.collection = testdb.datasources

 

-------------------------------Mongodb 基本操作实现---------------------------

def insert_one(self, data):

res = BaseHandle.insert_one(self.collection, data)

return res

def insert_many(self, data_list):

res = BaseMG.insert_many(self.collection, data_list)

return res

def find_one(self, data, data_field={}):

res = BaseMG.find_one(self.collection, data, data_field)

return res

def find_many(self, data, data_fileld={}):

res = BaseMG.find_many(self.collection, data, data_fileld)

return res

def update_one(self,data_update, data_options):

res = BaseMG.update_one(self.collection, data_update, data_options)

return res

def update_many(self, data_update, data_options):

res = BaseMG.update_many(self.collection, data_update, data_options)

return res

def replace_one(self, data_replace, data_options):

res = BaseMG.replace_one(self.collection, data_replace, data_options)

return res

def delete_one(self, data):

res = BaseMG.delete_one(self.collection, data)

return res

def delete_many(self,data):

res = BaseMG.delete_many(self.collection, data)

return res

def find_all(self):

"""SELECT * FROM TABLE"""

return self.collection.find()

-------------------------------Mongodb 基本操作---------------------------

mongodb_operation.py

 

class BaseMG(object):

@staticmethod

def insert_one(collection, data):

res = collection.insert_one(data)

return res.inserted_id

 

@staticmethod

def insert_many(collection, data_list):

res = collection.insert_many(data_list)

return res.inserted_ids

 

@staticmethod

def find_one(collection, data, data_field={}):

if len(data_field):

res = collection.find_one(data, data_field)

else:

res = collection.find_one(data)

return res

 

@staticmethod

def find_many(collection, data, data_field={}):

if len(data_field):

res = collection.find(data, data_field)

else:

res = collection.find(data)

return res

 

@staticmethod

def update_one(collection, data_update, data_options):

"""修改一条数据"""

res = collection.update_one(data_update, data_options)

return res

 

@staticmethod

def update_many(collection, data_update, data_options):

""" 修改多条数据 """

res = collection.update_many(data_update, data_options)

return res

 

@staticmethod

def replace_one(collection, data_replace, data_option):

""" 完全替换掉 这一条数据, 只是 _id 不变"""

res = collection.replace_one(data_replace, data_option)

return res

 

@staticmethod

def delete_one(collection, data):

res = collection.delete_one(data)

return res

 

@classmethod

def delete_many(cls,collection, data):

res = collection.delete_many(data)

return res

 

-------------------------------Mongodb 基本操作实现---------------------------

只能单条检测,将注释去掉即可,数据库文件自己建立即可

test_mongodb.py

from db.mongodb_api import MongodbAPI

 

t = MongodbAPI()

# 插入一条数据

# document = {'target_id': '1111', 'solution': 'Delete item'}

# t.insert_one(document)

 

# 插入多条数据

# data_list = [{'target_id':'222', 'solution': 'overwrite'},{'target_id':'333','solution':'fixed home'}]

# t.insert_many(data_list)

 

# 查找一条数据并显示

# result = t.find_one({'target_id':'222'}, {'target_id': 1})

# print(result)

 

# 查找多条数据并显示

# result = t.find_many({'target_id':'222'}, {'target_id': 1})

# for res in result:

# print(res)

 

# 更新一条数据

# try:

# t.update_one(

# {'target_id':'222'},

# {'$set':{'solution':'update'}}

# )

# except Exception as e:

# print(e)

 

# 更新多条数据

# try:

# t.update_many(

# {'target_id': '222'},

# {'$set':{'solution':'update many'}}

# )

# except Exception as e:

# print(e)

 

# 替换一条数据

# try:

# t.replace_one(

# {'target_id': '222'},

# {'target_id':'222', 'new_customer': 'yes'}

# )

# except Exception as e:

# print(e)

 

# 删除一条数据

# try:

# t.delete_one({'target_id': '222'})

# except Exception as e:

# print(e)

 

# 删除多条数据

# try:

# t.delete_many({'target_id': '333'})

# except Exception as e:

# print(e)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值