Python操作Mongodb資料庫
1 、安裝 pymongo 庫
# pip3 install pymongo
2 、操作 mongo 資料庫
import pymongo
client=pymongo.MongoClient("mongodb:// 使用者名稱 : 密碼 @ip:27017/") # 如 mongodb://admin:123456@192.168.221.128:27017
# 指定資料庫
db = client['albaba']
# 指定集合
collection = db['shops']
# 新增資料 , 欄位 :id 為 3394
collection.insert_one({"id": 3394})
# 查詢資料 find_one(),find()
result = collection.find_one({"id": 3394})
# 查詢一條 id 為 3394 的資訊
print(result)
# 查詢所有 category 欄位為果蔬生鮮 / 蔬菜的值
results = collection.find({"category": " 果蔬生鮮 / 蔬菜 "})
for one in results:
print(one)
# 更新資料 , 把 id=3394 的資料裡的 name 屬性值換成鮮花
collection.update_one(({"id": 3394}, {'$set': {'name': " 鮮花 "}}))
# 刪除資料 ,外匯跟單gendan5.com 刪除一條 id 為 3394 的資料
collection.delete_one({'id': 3394})
# 刪除資料 , 刪除所有 category 欄位值為果蔬生鮮 / 蔬菜
collection.delete_many({"category": " 果蔬生鮮 / 蔬菜 "})
3 、可以封裝成類,和對應的方法
import pymongo
class MongoDB:
def __init__(self, user='admin', pwd='123456', ip='1192.168.221.128', port=27017):
'''
:param user: 使用者名稱
:param pwd: 密碼
:param ip: ip 地址
:param port: 埠號
'''
self.user = user
self.pwd=pwd
self.ip=ip
self.port=port
self.client = pymongo.MongoClient(f"mongodb://{user}: {pwd}@{ip}:{port}/")
def insert(self, collection, query, db='albaba'):
'''
:param db: 使用的資料庫
:param collection: 使用的集合
:param query: 新增的資訊
:return:
'''
db = self.client[db]
collection = db[collection]
collection.insert_one(query)
def find_one(self, collection, query, db='albaba'):
'''
:param db: 使用的資料庫
:param collection: 使用的集合
:param query: 查詢的資訊
:return: 查詢結果
'''
db = self.client[db]
collection = db[collection]
result = collection.find_one(query)
return result
def find_all(self, collection, query, db='albaba'):
'''
:param db: 使用的資料庫
:param collection: 使用的集合
:param query: 查詢的資訊
:return: 查詢返回所有結果
'''
db = self.client[db]
collection = db[collection]
results = collection.find(query)
return results
def update_one(self, collection, myquery, newvalues, db='albaba'):
'''
:param db: 使用的資料庫
:param collection: 使用的集合
:param myquery: 查詢的資訊
:param newvalues: 修改的資訊
:return:
'''
db=self.client[db]
collection = db[collection]
collection.update_one(myquery, {'$set': newvalues})
def delete_one(self, collection, query, db='albaba'):
'''
:param db: 使用資料庫
:param collection: 使用的集合
:param query: 資訊
:return:
'''
db = self.client[db]
collection = db[collection]
collection.delete_one(query)
def delete_all(self, collection, query, db='albaba'):
'''
:param db: 使用資料庫
:param collection: 使用的集合
:param
query: 資訊
:return:
'''
db = self.client[db]
collection = db[collection]
collection.delete_many(query)
if __name__ == '__main__':
db = MongoDB()
res = db.find_one('albaba', 'shops', {'phone': '13500000822'})
print(res)
db.update_one('albaba', 'shops', {'phone': '13500000822'}, {'phone': '13500000833'})
res1 = db.find_one('albaba', 'shops', {'id': 1})
print(res1)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2770529/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python操作MongoDB文件資料庫PythonMongoDB資料庫
- Python資料庫MongoDB騷操作Python資料庫MongoDB
- Python 資料庫騷操作 -- MongoDBPython資料庫MongoDB
- MongoDB 資料庫操作MongoDB資料庫
- mongodb資料庫操作MongoDB資料庫
- nodejs操作mongodb資料庫(mongodb)NodeJSMongoDB資料庫
- MongoDB資料庫基礎操作MongoDB資料庫
- Python3.5使用pymongo(3.3.1)操作mongodb資料庫PythonMongoDB資料庫
- python資料庫-mongoDB的高階查詢操作(55)Python資料庫MongoDB
- MongoDB資料庫的基本操作梳理MongoDB資料庫
- ASP.NET MongoDB資料庫操作類ASP.NETMongoDB資料庫
- MongoDB資料庫操作詳解:基礎篇MongoDB資料庫
- Python操作SQLite資料庫PythonSQLite資料庫
- Python 操作 SQLite 資料庫PythonSQLite資料庫
- python資料庫(mysql)操作Python資料庫MySql
- python 操作mysql資料庫PythonMySql資料庫
- Python Mysql 資料庫操作PythonMySql資料庫
- python操作mysql資料庫PythonMySql資料庫
- mongodb資料庫使用03、python和mongodb的互動MongoDB資料庫Python
- node 使用 monk 工具操作 mongodb 資料庫學習MongoDB資料庫
- mongodb資料庫MongoDB資料庫
- Python全棧 MongoDB 資料庫(資料的查詢)Python全棧MongoDB資料庫
- Python全棧MongoDB資料庫(資料的查詢)Python全棧MongoDB資料庫
- python資料庫-MongoDB的安裝(53)Python資料庫MongoDB
- MongoDB之資料增加操作MongoDB
- python+資料庫(三)用python對資料庫基本操作Python資料庫
- Python 資料庫騷操作 — RedisPython資料庫Redis
- Python 資料庫騷操作 -- RedisPython資料庫Redis
- Python之 操作 MySQL 資料庫PythonMySql資料庫
- Eolink Apikit :資料字典功能上線、支援 MongoDB 資料庫操作...APIMongoDB資料庫
- MongoDB之資料查詢操作MongoDB
- 常見MongoDB資料庫操作產生的鎖總結MongoDB資料庫
- Python操作三大主流資料庫Python資料庫
- Python操作Redis快取資料庫PythonRedis快取資料庫
- python資料庫操作 - PyMySQL入門Python資料庫MySql
- 01-python操作Mysql資料庫PythonMySql資料庫
- python sqlite3 資料庫操作PythonSQLite資料庫
- Python資料分析庫pandas基本操作Python