MongoDBの4.python
データベースに接続し、接続を閉じます。
import pymongo
def connection_db():
connection = pymongo.MongoClient("127.0.0.1", 27017) # 连接本地mongodb库
tdb = connection["ticket"] # 连接ticket库 也可写为 tdb = connection.ticket
return connection, tdb
def close_connection(connection):
connection.close() # 关闭连接
挿入データ
def insert_one_data(tdb, collection, mongodb_data):
insert_x = tdb[collection].insert_one(mongodb_data) # 插入一条数据
return insert_x
def insert_many_data(tdb, collection, mongodb_data):
insert_x = tdb[collection].insert_many(mongodb_data) # 插入多条数据
return insert_x
if "__name__" == "main":
conn, tdb = connection_db()
res = insert_many_data(tdb, "ticket_info", data)
print(res._InsertManyResult__inserted_ids)
print( res.acknowledged)
close_connection(conn)
出力:
[ObjectId('5e6f45309057eba36a8b2471'), ObjectId('5e6f45309057eba36a8b2472'), ObjectId('5e6f45309057eba36a8b2473'), ObjectId('5e6f45309057eba36a8b2474'), ObjectId('5e6f45309057eba36a8b2475')]
True
クエリデータ:
from bson.objectid import ObjectId
## 若要查询mongodb自动生成的 _id 为查询条件 需要上面的ObjectId包
def select_one_data(tdb, collection, filters):
info = tdb[collection].find_one(filters) # 查询单条数据
return info
def select_all_data(tdb, collection, filters): # 查询多条数据,返回可迭代对象
info = tdb[collection].find(filters)
return info
if "__name__" == "main":
conn, tdb = connection_db()
filter = {"_id": ObjectId("5e6f45309057eba36a8b2471")}
res = select_all_data(tdb, "ticket_info", filter)
for item in res:
print(item)
close_connection()
出力:
{'_id': ObjectId('5e6f45309057eba36a8b2471'), 'item': 'journal', 'qty': 25, 'tags': ['blank', 'red'], 'dim_cm': [14, 21]}
クエリ:
需要条件 | 変換例の後 | リマーク |
---|---|---|
ページ= 1、サイズ= 10 | tdb.ticket_info.find(フィルター).limit(サイズ).SKIP(サイズ*(ページ - 1)) | |
数量> 40 | フィルタ= { "数量":{ "$ GT":40}} | |
数量= 25 | フィルタ= { "数量":25} | 或フィルタ= { "数量":{ "$ね":25}} |
数量<40 | フィルタ= { "数量":{ "$ LT":40}} | |
数量> = 40 | フィルタ= { "数量":{ "$ GTE":40}} | |
数量<= 40 | フィルタ= { "数量":{ "$ LTE":40}} | |
[25、45]で数量 | フィルタ= { "数量" { "で$" [25、45]}} | |
10と100 berween数量 | フィルタ= { "数量":{ "$ GTE":10、 "$ LTE":100}} | |
"%pが%" などの項目 | フィルタ= { "項目":{ "$正規表現": "P"}} | それがpで終わる場合、 "P $";、 "^ P" Pで始まる場合は |
{: 'Z3'、 "子供":[{ "名": "ZQ"、 "年齢":10}、{ "名前": "ZW"、 "年齢":11}] "名"}あなたがしている場合このタイプのデータ、あなたがリストを検索したいデータ | フィルタ= { "name.name": "ZQ"} | |
ソーティング、フラッシュバックは-1、1は上記実施例に正のシーケンス、データ型であります | query_result.sort( "名前"、1).sort( "name.age"、-1) | ソートフィールドの数、プラスsortメソッドの数 |
結果の数 | query_result.count() | オブジェクトの完全な実行に方法を見つけることのようquery_result |
あなただけの特定のフィールドを表示する、またはいくつかのフィールドを返すようにしたい場合 | ({0、 "数量" 1 "_id"}フィルタ、)を見つけます | フィールド戻り値:最初のパラメータ検索()は、検索条件のdict型、開発の分野および禁止返された情報が「_id」の一例である第2のパラメータを定義しているフィールドは、「数量」を返しません |
注意:
スキップ()、limilt()、ソート()を実行する3つ)の実行の順序が(ソートする場合)をスキップ(、)を一緒に入れ、最後に(に示すリミット。
データを更新します。
def update_one_data(tdb, collection, my_query, new_values): # 更新一条数据
tdb[collection].update_one(my_query, new_values)
def update_many_data(tdb, collection, my_query, new_values): # 更新多条数据
tdb[collection].update_many(my_query, new_values)
データを削除します。
def delete_one_data(tdb, collection, filters):
tdb[collection].delete_one(filters)
def delete_many_data(tdb, collection, filters):
tdb[collection].delete_many(filters)