Simple pymongo paginator using bucket pattern
Project description
py-mongonnator
Just a simple production-ready mongo paginator written in Python for PyMongo package using bucket pattern.
Installation
pip install PyMongonnator
Usage
PyMongonnator
exposes two ways to paginate over collections:
- Built-in API importing overriden MongoClient class.
- Explicit API passing a MongoClient collection into Paginator method.
Options
query
: a SON object specifying elements which must be present for a document to be included in the result set. Default is{}
(query all).limit
: Number of documents per page. Default is75
.ordering_case
: Ordering sense when retrieving documents from mongo. Valid options are:ASCENDING
: Ascending sort order.DESCENDING
: Descending sort order (default).
ordering_field
: Field to order collections. Default is_id
.projection
: a dict specifying the fields to include or exclude. Please note that the id cannot be excluded because is mandatory in pagination. Default is{}
(include all).prev_page
: Previous pagination pointer. When no previous page is available, will be None. Default isNone
.next_page
: Next pagination pointer. When no next page is available, will be None. Default isNone
.automatic_pagination
: If you want to paginate automatically in batches oflimit
over entire collection. Default isTrue
.
When options are set, they should remain unchanged during the whole pagination process except pagination pointers (prev_page/next_page).
Built-in API (recommended)
from mongonator import MongoClientWithPagination, ASCENDING
MONGO_URI = 'mongodb://[user]:[password]@[host]:[port]/admin'
DATABASE = 'database'
COLLECTION = 'collection'
# Instantiate mongo client with pagination
mongo_client = MongoClientWithPagination(MONGO_URI)
db = mongo_client[DATABASE]
col = db[COLLECTION]
query_filter = {'name': {'$ne': None}}
# Paginate automatically in batches of 5
for d in col.paginate(query=query_filter, limit=5, projection={'email': 1, 'name': 1},
ordering_field='name', ordering=ASCENDING):
print(d.response)
print(d.batch_size)
# Paginate manually in batches of 5
page = col.paginate(query=query_filter, limit=5, projection={'email': 1, 'name': 1},
ordering_field='name', ordering=ASCENDING, automatic_pagination=False)
# ahead (next five documents)
next_batch_of_five = col.paginate(query=query_filter, limit=5, projection={'email': 1, 'name': 1},
ordering_field='name', ordering=ASCENDING, automatic_pagination=False, next_page=page.next_page)
# back (prev five documents from next_batch_of_five situation)
prev_batch_of_five = col.paginate(query=query_filter, limit=5, projection={'email': 1, 'name': 1},
ordering_field='name', ordering=ASCENDING, automatic_pagination=False, next_page=next_batch_of_five.prev_page)
This method is intended when you just started a new project from scratch or for existing projects if you are willing to substitute every MongoClient
instance for MongoClientWithPagination
.
Explicit API
from mongonator import Paginate, ASCENDING
from pymongo import MongoClient
MONGO_URI = 'mongodb://[user]:[password]@[host]:[port]/admin'
DATABASE = 'database'
COLLECTION = 'collection'
query_filter = {'name': {'$ne': None}}
# Instantiate MongoClient from pymongo
with MongoClient(MONGO_URI) as mongo_client:
db = mongo_client[DATABASE]
col = db[COLLECTION]
# Manual pagination in batches of 2
paginator = Paginate(
collection=col,
query=query_filter,
limit=2,
ordering_field='email',
ordering_case=ASCENDING,
projection={'email': 1, 'name': 1},
automatic_pagination=False
).paginate()
# Print results
print("Response: ", paginator.response)
print("Prev page: ", paginator.prev_page)
print("Next page: ", paginator.next_page)
print("Batch size: ", paginator.batch_size)
# Manual pagination for two next results...
paginator = Paginate(
collection=col,
query=query_filter,
limit=2,
ordering_field='email',
ordering_case=ASCENDING,
projection={'email': 1, 'name': 1},
automatic_pagination=False,
next_page=paginator.next_page,
).paginate()
# Print results
print("Response: ", paginator.response)
print("Prev page: ", paginator.prev_page)
print("Next page: ", paginator.next_page)
print("Batch size: ", paginator.batch_size)
# ... Or simply use automatic pagination in batches of 2 (starting in first document)
for d in Paginate(
collection=col,
query=query_filter,
limit=2,
ordering_field='email',
ordering_case=ASCENDING,
projection={'email': 1, 'name': 1},
automatic_pagination=True,
).paginate():
print(d.response)
This method is intended when you have a big project in production and is not possible to substitute every MongoClient call.
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
Built Distribution
File details
Details for the file PyMongonnator-1.0.0.tar.gz
.
File metadata
- Download URL: PyMongonnator-1.0.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3d3c6b5a5a02d656a556f696323c7cb299f80443ddc1495803c899fdb6e59fa |
|
MD5 | 1b7efef4c9106f10a3411d39f31baf07 |
|
BLAKE2b-256 | 96ceb59c16d3c90003890ec5aa6f7a896c4f95b38ffa996f35d81f802b878b21 |
File details
Details for the file PyMongonnator-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: PyMongonnator-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40c4d9869945141c305f70074d86b093fc38b48d40abad4fa074bceffa39a014 |
|
MD5 | 39d17f05e55937aa3fe835411b454d1f |
|
BLAKE2b-256 | b15444d267ed2b1933ef85a805cc962a77cfdf64441c66d0b593d24d047ecb81 |