Skip to main content

Paging support for Mongo

Project description

Pageable Mongo

Paging support for Mongo

Latest Version on PyPI Supported Implementations Built with PyPi Template

What is this?

This is merely a quick implementation and packaging of a way to use Mongo features to produce a Pageable-lookalike dictionary with information regarding the query that was performed. It is aimed at supporting applications that want to query in a pages way.

All credits go to https://stackoverflow.com/a/53220591, and even more to the Mongo developers ;-)

Minimal Survival Commands

>>> import random
>>> import json
>>> 
>>> from pymongo import MongoClient
>>> from pageable_mongo import Pageable
>>> 
>>> mongo = MongoClient()
>>> db    = mongo["test"]
>>> 
>>> # generate some documents
>>> db["collection"].drop()
>>> values = [ "value 1", "value 2", "value 3", "value 4" ]
>>> for _ in range(10000):
...   result =db["collection"].insert_one({ "key" : random.choice(values) })
... 
>>> def query(db):
...   return db["collection"].find(
...     { "key" : { "$in" : [ "value 1", "value 4" ] } },
...     { "_id" : False }
...   ).sort("key", -1).skip(15).limit(10)
... 
>>> # classic query
>>> rows = query(db)
>>> print(json.dumps(list(rows), indent=2))
[
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  },
  {
    "key": "value 4"
  }
]
>>> # paged query
>>> pageable = query(Pageable(db))
>>> print(json.dumps(pageable.query,  indent=2))
[
  {
    "$match": {
      "key": {
        "$in": [
          "value 1",
          "value 4"
        ]
      }
    }
  },
  {
    "$facet": {
      "resultset": [
        {
          "$project": {
            "_id": false
          }
        },
        {
          "$sort": {
            "key": -1
          }
        },
        {
          "$skip": 15
        },
        {
          "$limit": 10
        }
      ],
      "total": [
        {
          "$count": "count"
        }
      ]
    }
  },
  {
    "$project": {
      "resultset": "$resultset",
      "total": {
        "$arrayElemAt": [
          "$total",
          0
        ]
      }
    }
  },
  {
    "$project": {
      "content": "$resultset",
      "totalElements": "$total.count"
    }
  }
]
>>> print(json.dumps(pageable.result, indent=2))
{
  "content": [
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    },
    {
      "key": "value 4"
    }
  ],
  "totalElements": 4906,
  "pageable": {
    "sort": {
      "sorted": true,
      "unsorted": false,
      "empty": false
    },
    "offset": 15,
    "pageNumber": 1,
    "pageSize": 10,
    "paged": true,
    "unpaged": false
  },
  "first": false,
  "last": false,
  "totalPages": 491,
  "numberOfElements": 10,
  "number": 15,
  "size": 10,
  "empty": false,
  "sort": {
    "sorted": true,
    "unsorted": false,
    "empty": false
  }
}

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

pageable-mongo-0.0.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

pageable_mongo-0.0.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file pageable-mongo-0.0.1.tar.gz.

File metadata

  • Download URL: pageable-mongo-0.0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.12

File hashes

Hashes for pageable-mongo-0.0.1.tar.gz
Algorithm Hash digest
SHA256 984a842c26e671308eb32dce926dfb3991b1ae6a9d0947bf6415340168df8ae4
MD5 de34eee4c1083d3f356d6ae634c3d15a
BLAKE2b-256 d55cba7c344612daf287cbbba44da3a2b9a2ef7a6916bc1babdb6cc25eb7e5b8

See more details on using hashes here.

File details

Details for the file pageable_mongo-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pageable_mongo-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.12

File hashes

Hashes for pageable_mongo-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2e771e56d78dea03ced7146cce42e4c1585933c9594819055e4dd3531d40e61
MD5 09189b7e0dcd0390be5318c1f00827ca
BLAKE2b-256 a5215e49eaa9f428dc4e8f97d9b23b6a04110e6b9d3b7ffefecf754fc9f2da91

See more details on using hashes here.

Supported by

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