motor基礎知識學習

2021-10-17 20:48:29 字數 2554 閱讀 7490

motor 資料庫插入

# -*- coding: utf-8 -*-

# motor是乙個非同步mongodb driver,支援非同步讀寫mongodb。

# motor同時支援使用asyncio(python3.4以上標準庫)作為非同步模型,使用起來十分方便。

from motor import motor_asyncio

import asyncio

async def init():

#將asynciomotorclient 放單獨函式中,便於呼叫

client = motor_asyncio.asynciomotorclient('127.0.0.1',27017)

db = client['motor']

# 建立或連線集合(表)

collection = db['learn']

return collection

async def insert_docment(jihe):

doc =

res = await jihe.insert_one(doc)

async def main():

collection = await init()

await insert_docment(collection)

asyncio.run(main())

motor 查詢操作

# -*- coding: utf-8 -*-

from motor import motor_asyncio

import asyncio

async def init():

client = motor_asyncio.asynciomotorclient('127.0.0.1',27017)

db = client['aitext']

collection = db['aa']

return collection

async def find_one_data(colleciton):

result = await colleciton.find_one(,)

print(result)

# limit限制條數 skip 跳過條數

async def find_all_data(colleciton,limit = 10 ,skip = 1):

# 第一種查詢方法

# cursor = colleciton.find({},, skip = skip)

# for item in await cursor.to_list(length = limit):

# print(item)

# 第二種查詢方法

async for item in colleciton.find({},, skip = skip):

print(item)

async def main():

colleciton = await init()

# await find_one_data(colleciton)

await find_all_data(colleciton)

asyncio.run(main())

motor 統計,更新,刪除

async def init():

# 初始化motor

client = motor_asyncio.asynciomotorclient('127.0.0.1', 27017)

db = client['aitext']

colleciton = db['kanchai']

return colleciton

# 統計資料庫條數

async def count_docment(client):

counts = await client.count_documents()

print(counts)

# 更新資料庫內容

async def updata_docment(client):

# update = await client.update_one(,})

update = await client.update_many(,})

print(update.modified_count)

# 刪除資料庫內容

async def delete_docment(client):

# delete = await client.delete_one()

delete = await client.delete_many()

print(delete.deleted_count)

async def main():

# 主邏輯

client = await init()

# await count_docment(client)

# await updata_docment(client)

await delete_docment(client)

asyncio.run(main())

基礎知識學習

format 字串格式化的一種方式 10月 日 format 1 10月 日 format 1,2,3 10月 日 format 月 日 format 1,2,3 月 日 format 1 一 列表推導式 幫助快速的生成包含一堆資料的列表 i 10 for i in range 10 10 11,1...

QT學習 基礎知識

qt把它所支援的平台分兩級,第一級是重點支援的,第二級次之 第一級平台 platform compilers linux 32 and 64 bit gcc 4.2 microsoft windows xp gcc 4.4 mingw 32 bit msvc 2003,2005 32 and 64 ...

機器學習基礎知識

machine learning的主要兩個應用方面 人工智慧 資料科學。什麼是人工智慧 artificial intelligence ai是不確定性管理 uncertainty management 的體現 ai what to do when you don t know what to do ...