python操作mongo例項

2022-07-17 00:45:14 字數 4653 閱讀 5792

#

coding:utf-8

"""mongo操作工具

"""from pymongo import

mongoclient

mongo_host, mongo_port, mongo_db, mongo_table = '

127.0.0.1

', '

27017

', '

test_db

', '

teat_tb

'class

mongoutils:

"""鏈結mongodb,進行各種操作

"""def

__init__(self, host=mongo_host, port=mongo_port, db_name=mongo_db):

"""初始化物件,鏈結資料庫

:param host: mongo資料庫所在伺服器位址

:param port: mongo資料庫埠

:param db_name: 資料庫的名稱

:return: 無返回值

"""try

: self.client =none

self.client =mongoclient(host, port)

self.database =self.client.get_database(db_name)

self.collection =none

except

exception as e:

self.close_conn()

print('

init mongo bar failed: %s

' %e)

def change_collection(self, table_name=mongo_table):

"""切換表

"""self.collection =self.database.get_collection(table_name)

def count_info(self, table_name=mongo_table, filter_dict=none):

"""查詢表記錄條數,預設返回0

:param table_name: str 表名

:param filter_dict: dict 過濾條件

:return: int 表記錄條數

"""tab_size =0

try:

self.collection =self.database.get_collection(table_name)

tab_size =self.collection.find(filter_dict).count()

return

tab_size

except

exception as e:

print('

get table size failed: %s

' %e)

finally

:

return

tab_size

def update_info(self, filter_dict, update_dict, insert=false, multi=false):

"""更新表記錄,預設返回false

:param filter_dict: dict 過濾條件,如}

:param update_dict: dict 更新的字段,如,}

:param insert: bool 如果需要更新的記錄不存在是否插入

:param multi: bool 是否更新所有符合條件的記錄, false則只更新一條,true則更新所有

:return: bool 是否更新成功

"""result =false

try:

self.collection.update(filter_dict, update_dict, insert, multi)

result =true

print("

[info] update success!")

except

exception as e:

print('

update failed: %s

' %e)

finally

:

return

result

definsert_info(self, insert_date):

"""更新表記錄,預設返回false

:param insert_date: dict 插入的資料,如}

:return: bool 是否更新成功

"""result =false

try:

self.collection.insert(insert_date)

result =true

print("

insert success!")

except

exception as e:

print('

insert failed: %s

' %e)

finally

:

return

result

defdelete_info(self, filter_date):

"""更新表記錄,預設返回false

:param filter_date: dict 刪除資料的條件,如}

:return: bool 是否更新成功

"""result =false

try:

self.collection.remove(filter_date)

result =true

print("

remove success!")

except

exception as e:

print('

remove failed: %s

' %e)

finally

:

return

result

deffind_one_info(self, filter_dict, return_dict):

"""查詢一條表記錄,預設返回空字典

:param filter_dict: dict 過濾條件如

:param return_dict: dict 返回的字段如

:return: dict 查詢到的資料

"""result ={}

try:

result =self.collection.find_one(filter_dict, return_dict)

except

exception as e:

print('

find data failed: %s

' %e)

finally

:

return

result

def find_multi_info(self, filter_dict, return_dict, limit_size=0, skip_index=0):

"""查詢多條表記錄,預設返回空陣列

:param filter_dict: dict filter_dict: 過濾條件如

:param return_dict: dict 返回的字段如

:param limit_size: int 限定返回的資料條數

:param skip_index: int 游標位移

:return: list 查詢到的記錄組成的列表,每個元素是乙個字典

"""result =

try:

ifnot

limit_size:

ifnot

skip_index:

result =self.collection.find(filter_dict, return_dict)

else

: result =self.collection.find(filter_dict, return_dict).skip(skip_index)

else

:

ifnot

skip_index:

result =self.collection.find(filter_dict, return_dict).limit(limit_size)

else

: result =self.collection.find(filter_dict, return_dict).skip(skip_index).limit(limit_size)

except

exception as e:

print('

find data failed: %s

' %e)

finally

:

return

result

defclose_conn(self):

"""關閉資料庫鏈結

:return: 無返回值

"""if

self.client:

self.client.close()

mongo簡單操作

use admin 進入資料庫admin db.adduser name pwd 增加或修改使用者密碼 db.system.users.find 檢視使用者列表 db.auth name pwd 使用者認證 db.removeuser name 刪除使用者 show users 檢視所有使用者 sh...

mongo基礎操作

1.linux伺服器中已經安裝了mongo後,在mongo命令下的基礎shell命令 show dbs 顯示資料庫列表 show collections 顯示當前資料庫中的集合 類似關聯式資料庫中的表 show users 顯示使用者 use 切換當前資料庫,這和ms sql裡面的意思一樣 db.h...

mongo多表查詢例項講解

這裡主要是說 lookuplookup就是使用aggregate的 lookup屬性 user表 ordersitem 表 lookuplookup作為內嵌試用是比較耗費時間的 我們應該盡力避免使用,資料量比較大的情況下經常會導致查詢失敗 因本人也是初學 所以以部落格的形式記錄下最近的使用心得 上面...