Docs Menu
Docs Home
/
드라이버
/

Motor(비동기 드라이버)

비동기 Python 애플리케이션을 위한 공식 MongoDB 운전자 모터 설명서 사이트 에 오신 것을 환영합니다. pip 를 사용하여 다운로드하거나 튜토리얼에 따라 실행 가능한 프로젝트 설정하다 .

비차단 방식으로 또는 코루틴에서 MongoDB에 접근할 필요가 없다면 대신 PyMongo 드라이버를 사용하는 것이 좋습니다.

Motor 드라이버의 특정 사용 사례를 설명하는 블로그 게시물을 읽으려면 아래 링크를 따르세요.

Python 애플리케이션 에서 사용할 수 있도록 하려면 모터 운전자 모듈을 설치해야 합니다. pip 를 사용하여 모터 설치하는 것이 좋습니다.

다음 명령은 명령줄을 사용하여 최신 버전 모듈을 설치하는 방법을 보여줍니다.

$ python -m pip install motor

요구 사항 및 기타 설치 방법에 대한 자세한 내용은 Motor 설치 문서를 참조하세요.

다음 연결 스니펫을 사용하여 asyncio비동기 프레임워크를 사용한 Atlas의 MongoDB deployment에 대한 연결을 테스트할 수 있습니다.

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())

연결 스니펫은 Stable API(Stable API) 기능을 사용합니다. 이 기능은 Motor 드라이버 v2.5 이상 사용 시 활성화할 수 있으며 MongoDB Server v5.0 이상에 연결할 때에 사용됩니다. 이 기능을 사용하면 Stable API에서 다루는 모든 명령의 하위 호환성 문제에 대한 걱정 없이 드라이버나 서버를 업데이트할 수 있습니다.

Stable API 기능에 대해 자세히 알아보려면 서버 매뉴얼에서 Stable API를 참조하세요.

참고

2022년 2월부터 버전이 지정된 APIStable API로 표시됩니다. 이름은 변경되나 모든 개념과 기능은 동일하게 유지됩니다.

Stable API 기능에 대한 지원이 없는 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 deployment에 연결하기 위해 다음 코드를 사용할 수 있습니다:

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 서버를 실행해야 하는 경우, 다음을 완료해야 합니다:

  1. MongoDB Server의 Community 또는 Enterprise 버전을 다운로드하세요.

  2. MongoDB 서버를 설치하고 구성합니다.

  3. 서버를 시작합니다.

중요

악의적인 공격으로부터 항상 MongoDB 서버를 보호하세요. 보안 체크리스트에서 보안 권장 사항 목록을 확인하세요.

MongoDB Server를 성공적으로 시작한 후에는 드라이버 연결 코드에 연결 문자열을 지정하세요.

MongoDB Server가 로컬에서 실행되는 경우 연결 문자열("mongodb://localhost:<port>")을 사용할 수 있습니다. 여기서 <port>는 서버가 들어오는 연결을 수신하도록 구성한 포트 번호입니다.

다른 호스트 이름 또는 IP 주소를 지정해야 하는 경우 연결 문자열에 대한 서버 매뉴얼 항목을 참조하세요.

서버에 연결할 수 있는지 테스트하려면 MongoDB Atlas에 연결 코드 예시에서 연결 문자열을 바꾸고 실행하세요.

Motor의 MongoDB Server 및 Python 호환성에 대한 자세한 내용은 호환성 페이지를 참조하세요.

돌아가기

버전 호환성

이 페이지의 내용