Docs Menu
Docs Home
/
ドライバー
/

Motor(非同期ドライバー)

非同期Pythonアプリケーション用の公式MongoDBドライバーであるMotorのドキュメント サイトへようこそ。pip を使用してダウンロードするか、チュートリアルに従って実行可能なプロジェクトをセットアップしてください。

Tip

非ブロッキング方式またはコルーチンから MongoDB にアクセスする必要がない場合は、代わりに PyMongo ドライバーを使用することをお勧めします。

以下のリンクから、Motor ドライバーの具体的なユースケースを説明したブログ記事をお読みください。

Motor ドライバー モジュールを Python アプリケーションで使用できるようにするには、Motor ドライバー モジュールをインストールする必要があります。Motor をインストールするには、pip の使用をお勧めします。

以下のコマンドは、コマンドラインを使用してモジュールの最新バージョンをインストールする方法を示しています。

$ python -m pip install motor

要件やその他のインストール方法の詳細については、Motor のインストール ドキュメントを参照してください。

次の接続スニペットを使用して、asyncio 非同期フレームワークを使用して Atlas 上の MongoDB 配置への接続をテストできます。

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo.server_api import ServerApi
async def ping_server():
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Set the Stable API version when creating a new client
client = AsyncIOMotorClient(uri, server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
await client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
asyncio.run(ping_server())

この接続スニペットは、Motor ドライバー v2.5 以降を使用して MongoDB Server v5.0 以降に接続するときに有効にできる Stable API 機能を使用します。この機能を使用すると、Stable API でカバーされるコマンドの下位互換性の問題を心配することなく、ドライバーまたはサーバーをアップデートできます。

Stable API 機能の詳細については、サーバー マニュアルの「Stable API」を参照してください。

注意

2022 年 2 月以降、Versioned APIStable API という名前に変更しました。この名前の変更に伴うコンセプトと機能の変更はありません。

Stable API 機能をサポートしていないバージョンの MongoDB またはドライバーを使用している場合は、次のコード スニペットを使用して、MongoDB Atlas 上の MongoDB 配置への接続をテストできます。

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
async def ping_server():
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Create a new client and connect to the server
client = AsyncIOMotorClient(uri)
# Send a ping to confirm a successful connection
try:
await client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
asyncio.run(ping_server())

tornado 非同期ライブラリを使用している場合は、次のコードを使用して MongoDB 配置に接続できます。

import tornado
import motor
async def ping_server():
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Set a 5-second connection timeout when creating a new client
client = motor.motor_tornado.MotorClient(uri, serverSelectionTimeoutMS=5000)
# Send a ping to confirm a successful connection
try:
await client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
tornado.ioloop.IOLoop.current().run_sync(ping_server)

Atlas クラスターを使用する代わりに、開発目的でローカル コンピューター上で MongoDB Server を実行する必要がある場合は、次の手順を完了する必要があります。

  1. MongoDB Server の Communityバージョン または Enterprise バージョンをダウンロードします。

  2. MongoDB Serverをインストールして構成します。

  3. サーバーを起動します。

重要

MongoDB サーバーを悪意のある攻撃から常に保護します。セキュリティ推奨事項のリストについては、「セキュリティ チェックリスト」を参照してください。

MongoDB Serverを正常に開始したら、ドライバー接続コードで接続文字列を指定します。

MongoDB Server がローカルで実行されている場合は、接続文字列 "mongodb://localhost:<port>" を使用できます。ここで、<port> は、着信接続をリッスンするようにサーバーに設定したポート番号です。

別のホスト名または IP アドレスを指定する必要がある場合は、接続文字列に関するサーバー マニュアルのエントリを参照してください。

サーバーに接続できるかどうかをテストするには、MongoDB Atlas への接続に記載のコード例の接続文字列を置き換えて実行します。

Motor のMongoDB ServerおよびPythonとの互換性については、 互換性 ページを参照してください。

戻る

バージョンの互換性

項目一覧