Skip to main content

Async MongoDB client for Python

Project description

Mongojet

CI Coverage Status PyPI version

Async (asyncio) MongoDB client for Python. It uses Rust MongoDB driver and tokio under the hood. Mongojet is 1.5-4x faster than Motor in simple benchmarks.

Requirements

  • Python >= 3.8
  • pymongo>=4.6.2 (only bson package is required)

Installation

pip install mongojet

Usage

Mongojet has an API similar to PyMongo/Motor (but not exactly the same)

Creating a Client

Typically, you should create a single instance of Client per application/process. All client options should be passed via MongoDB connection string.

from mongojet import create_client, ReadPreference

client = await create_client('mongodb://localhost:27017/test_database?maxPoolSize=16')

Getting a Database

default database

db = client.get_default_database()

database with specific name

db = client.get_database('test_database')

database with specific name and options

db = client.get_database('test_database', read_preference=ReadPreference(mode='secondaryPreferred'))

Getting a Collection

collection = db['test_collection']

with options

collection = db.get_collection('test_collection', read_preference=ReadPreference(mode='secondary'))

Inserting documents

insert_one

document = {'key': 'value'}
result = await collection.insert_one(document)
print(result)
#> {'inserted_id': ObjectId('...')}

insert_many

documents = [{'i': i} for i in range(1000)]
result = await collection.insert_many(documents)
print(len(result['inserted_ids']))
#> 1000

Find documents

find_one (to get a single document)

document = await collection.find_one({'i': 1})
print(document)
#> {'_id': ObjectId('...'), 'i': 1}

find (to get cursor which is an async iterator)

cursor = await collection.find({'i': {'$gt': 5}}, sort={'i': -1}, limit=10)

you can iterate over the cursor using the async for loop

async for document in cursor:
    print(document)

or collect cursor to list of documents using to_list method

documents = await cursor.to_list()

find_many (to get list of documents in single batch)

documents = await collection.find_many({'i': {'$gt': 5}}, sort={'i': -1}, limit=10)

Counting documents

n = await collection.count_documents({'i': {'$gte': 500}})
print(n)
#> 500

Aggregating documents

cursor = await collection.aggregate(pipeline=[
    {'$match': {'i': {'$gte': 10}}},
    {'$sort': {'i': 1}},
    {'$limit': 10},
])
documents = await cursor.to_list()
print(documents)

Updating documents

replace_one

result = await collection.replace_one(filter={'i': 5}, replacement={'i': 5000})
print(result)
#> {'matched_count': 1, 'modified_count': 1, 'upserted_id': None}

update_one

result = await collection.update_one(filter={'i': 5}, update={'$set': {'i': 5000}}, upsert=True)
print(result)
#> {'matched_count': 0, 'modified_count': 0, 'upserted_id': ObjectId('...')}

update_many

result = await collection.update_many(filter={'i': {'$gte': 100}}, update={'$set': {'i': 0}})
print(result)
#> {'matched_count': 900, 'modified_count': 900, 'upserted_id': None}

Deleting documents

delete_one

result = await collection.delete_one(filter={'i': 5})
print(result)
#> {'deleted_count': 1}

delete_many

result = await collection.delete_many(filter={'i': {'$gt': 5}})
print(result)
#> {'deleted_count': 94}

Working with GridFS

bucket = db.gridfs_bucket(bucket_name="images")

with open('/path/to/my/awesome/image.png', mode='rb') as file:
    data = file.read()
    result = await bucket.put(data, filename='image.png', content_type='image/png')
    file_id = result['file_id']

with open('/path/to/my/awesome/image_copy.png', mode='wb') as file:
    data = await bucket.get_by_id(file_id)
    file.write(data)

await bucket.delete(file_id)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongojet-0.1.1.tar.gz (50.0 kB view hashes)

Uploaded Source

Built Distributions

mongojet-0.1.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (7.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

mongojet-0.1.1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.0 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mongojet-0.1.1-pp310-pypy310_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

mongojet-0.1.1-pp310-pypy310_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (8.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

mongojet-0.1.1-pp310-pypy310_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.7 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

mongojet-0.1.1-pp310-pypy310_pp73-manylinux_2_12_i686.manylinux2010_i686.whl (8.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ i686

mongojet-0.1.1-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl (7.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

mongojet-0.1.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.0 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mongojet-0.1.1-pp39-pypy39_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

mongojet-0.1.1-pp39-pypy39_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (8.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

mongojet-0.1.1-pp39-pypy39_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.7 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

mongojet-0.1.1-pp39-pypy39_pp73-manylinux_2_12_i686.manylinux2010_i686.whl (8.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ i686

mongojet-0.1.1-pp38-pypy38_pp73-manylinux_2_28_aarch64.whl (7.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

mongojet-0.1.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.0 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mongojet-0.1.1-pp38-pypy38_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.9 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

mongojet-0.1.1-pp38-pypy38_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (8.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

mongojet-0.1.1-pp38-pypy38_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.7 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

mongojet-0.1.1-pp38-pypy38_pp73-manylinux_2_12_i686.manylinux2010_i686.whl (8.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ i686

mongojet-0.1.1-cp312-none-win_amd64.whl (6.2 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

mongojet-0.1.1-cp312-none-win32.whl (5.4 MB view hashes)

Uploaded CPython 3.12 Windows x86

mongojet-0.1.1-cp312-cp312-manylinux_2_28_aarch64.whl (7.6 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

mongojet-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.0 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

mongojet-0.1.1-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.8 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

mongojet-0.1.1-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (8.8 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

mongojet-0.1.1-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.7 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARMv7l

mongojet-0.1.1-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.whl (8.5 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.12+ i686

mongojet-0.1.1-cp312-cp312-macosx_11_0_arm64.whl (6.0 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

mongojet-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl (6.4 MB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

mongojet-0.1.1-cp311-none-win_amd64.whl (6.2 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

mongojet-0.1.1-cp311-none-win32.whl (5.4 MB view hashes)

Uploaded CPython 3.11 Windows x86

mongojet-0.1.1-cp311-cp311-manylinux_2_28_aarch64.whl (7.5 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

mongojet-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

mongojet-0.1.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.9 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

mongojet-0.1.1-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (8.8 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

mongojet-0.1.1-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.7 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARMv7l

mongojet-0.1.1-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl (8.5 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.12+ i686

mongojet-0.1.1-cp311-cp311-macosx_11_0_arm64.whl (6.0 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

mongojet-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl (6.4 MB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

mongojet-0.1.1-cp310-none-win_amd64.whl (6.2 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

mongojet-0.1.1-cp310-none-win32.whl (5.4 MB view hashes)

Uploaded CPython 3.10 Windows x86

mongojet-0.1.1-cp310-cp310-manylinux_2_28_aarch64.whl (7.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

mongojet-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

mongojet-0.1.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.9 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

mongojet-0.1.1-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (8.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

mongojet-0.1.1-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARMv7l

mongojet-0.1.1-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl (8.5 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ i686

mongojet-0.1.1-cp310-cp310-macosx_11_0_arm64.whl (6.0 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

mongojet-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl (6.4 MB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

mongojet-0.1.1-cp39-none-win_amd64.whl (6.2 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

mongojet-0.1.1-cp39-none-win32.whl (5.4 MB view hashes)

Uploaded CPython 3.9 Windows x86

mongojet-0.1.1-cp39-cp39-manylinux_2_28_aarch64.whl (7.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ ARM64

mongojet-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

mongojet-0.1.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.9 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

mongojet-0.1.1-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (8.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

mongojet-0.1.1-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARMv7l

mongojet-0.1.1-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl (8.5 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

mongojet-0.1.1-cp38-none-win_amd64.whl (6.2 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

mongojet-0.1.1-cp38-none-win32.whl (5.4 MB view hashes)

Uploaded CPython 3.8 Windows x86

mongojet-0.1.1-cp38-cp38-manylinux_2_28_aarch64.whl (7.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.28+ ARM64

mongojet-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

mongojet-0.1.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.9 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ s390x

mongojet-0.1.1-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (8.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

mongojet-0.1.1-cp38-cp38-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARMv7l

mongojet-0.1.1-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl (8.5 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page