Skip to main content

MongoDB's unofficial Python implementation.

Project description

drawing

Monty, Mongo tinified. MongoDB implementated in Python !

Build Status Coverage Status Version Maintainability

Inspired by TinyDB and it's extension TinyMongo.

MontyDB is:

  • A tiny version of MongoDB, against to MongoDB 3.6.4 (4.0 soon)
  • Written in pure Python, testing on Python 2.7, 3.6, 3.7, PyPy, PyPy3.5
  • Literally serverless.
  • Similar to mongomock, but a bit more than that.

All those implemented functions and operators, should behaved just like you were working with MongoDB. Even raising error for same cause.

Install

pip install montydb

Requirements
  • pymongo (for bson)

Example Code

>>> from montydb import MontyClient
>>> col = MontyClient(":memory:").db.test
>>> col.insert_many([{"stock": "A", "qty": 6}, {"stock": "A", "qty": 2}])

>>> cur = col.find({"stock": "A", "qty": {"$gt": 4}})
>>> next(cur)
{'_id': ObjectId('5ad34e537e8dd45d9c61a456'), 'stock': 'A', 'qty': 6}

Develop Status

See Projects' TODO

Storage Engine Configurations

The configuration process only required on repository creation or modification.

Currently, one repository can only assign one storage engine.

  • Memory

Memory storage does not need nor have any configuration, nothing saved to disk.

>>> from montydb import MontyClient
>>> client = MontyClient(":memory:")
  • FlatFile

FlatFile is the default on-disk storage engine.

>>> from montydb import MontyClient
>>> client = MontyClient("/db/repo")

FlatFile config:

[flatfile]
cache_modified: 0  # how many document CRUD cached before flush to disk.
  • SQLite

SQLite is NOT the default on-disk storage, need configuration first before get client.

>>> from montydb import set_storage, MontyClient
>>> set_storage("/db/repo", storage="sqlite")
>>> client = MontyClient("/db/repo")

SQLite config:

[sqlite]
journal_mode: WAL

SQLite write concern:

>>> client = MontyClient("/db/repo",
>>>                      synchronous=1,
>>>                      automatic_index=False,
>>>                      busy_timeout=5000)

Utilities

  • montyimport

    Imports content from an Extended JSON file into a MontyCollection instance. The JSON file could be generated from montyexport or mongoexport.

    >>> from montydb import open_repo, utils
    >>> with open_repo("foo/bar"):
    >>>     utils.montyimport("db", "col", "/path/dump.json")
    >>>
    
  • montyexport

    Produces a JSON export of data stored in a MontyCollection instance. The JSON file could be loaded by montyimport or mongoimport.

    >>> from montydb import open_repo, utils
    >>> with open_repo("foo/bar"):
    >>>     utils.montyexport("db", "col", "/data/dump.json")
    >>>
    
  • montyrestore

    Loads a binary database dump into a MontyCollection instance. The BSON file could be generated from montydump or mongodump.

    >>> from montydb import open_repo, utils
    >>> with open_repo("foo/bar"):
    >>>     utils.montyrestore("db", "col", "/path/dump.bson")
    >>>
    
  • montydump

    Creates a binary export from a MontyCollection instance. The BSON file could be loaded by montyrestore or mongorestore.

    >>> from montydb import open_repo, utils
    >>> with open_repo("foo/bar"):
    >>>     utils.montydump("db", "col", "/data/dump.bson")
    >>>
    
  • MongoQueryRecorder

    Record MongoDB query results in a period of time. Requires to access databse profiler.

    This works via filtering the database profile data and reproduce the queries of find and distinct commands.

    >>> from pymongo import MongoClient
    >>> from montydb.utils import MongoQueryRecorder
    >>> client = MongoClient()
    >>> recorder = MongoQueryRecorder(client["mydb"])
    >>> recorder.start()
    >>> # Make some queries or run the App...
    >>> recorder.stop()
    >>> recorder.extract()
    {<collection_1>: [<doc_1>, <doc_2>, ...], ...}
    
  • MontyList

    Experimental, a subclass of list, combined the common CRUD methods from Mongo's Collection and Cursor.

    >>> from montydb.utils import MontyList
    >>> mtl = MontyList([1, 2, {"a": 1}, {"a": 5}, {"a": 8}])
    >>> mtl.find({"a": {"$gt": 3}})
    MontyList([{'a': 5}, {'a': 8}])
    

    You can dump it with monty_dump or read from monty_load

    >>> monty_dump("/path/dump.json", mtl)
    >>> MontyList(monty_load("/path/dump.json"))
    MontyList([1, 2, {'a': 1}, {'a': 5}, {'a': 8}])
    

drawing

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

montydb-1.1.0.tar.gz (47.9 kB view details)

Uploaded Source

Built Distributions

montydb-1.1.0-py3-none-any.whl (57.5 kB view details)

Uploaded Python 3

montydb-1.1.0-py2-none-any.whl (57.5 kB view details)

Uploaded Python 2

File details

Details for the file montydb-1.1.0.tar.gz.

File metadata

  • Download URL: montydb-1.1.0.tar.gz
  • Upload date:
  • Size: 47.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for montydb-1.1.0.tar.gz
Algorithm Hash digest
SHA256 94594ba681abb6d699dd3b208e9718cf805f4545dc1b96b828710d52ea1e23c4
MD5 73653a2d8975061b3442ec45398611dc
BLAKE2b-256 b95094d38e00c8652932e6b9feb7fe9b0b7d723656996af684714fa063b1c978

See more details on using hashes here.

Provenance

File details

Details for the file montydb-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: montydb-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 57.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for montydb-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 03ad561fcaa758f3878b2139809bc5c492a1512979b8a3132d486c9f64bc63a4
MD5 a9ef89a238bbefb6183d228dd28b1698
BLAKE2b-256 2b0c77de33df775e5ec4fec23e3e5cf4b2849f3370882f0310c309e93b2aa1bd

See more details on using hashes here.

Provenance

File details

Details for the file montydb-1.1.0-py2-none-any.whl.

File metadata

  • Download URL: montydb-1.1.0-py2-none-any.whl
  • Upload date:
  • Size: 57.5 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.15

File hashes

Hashes for montydb-1.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 f26cd39e916b75193b130369cf2d857c239e564d6e08aa348507808b683acb2f
MD5 31e44bb1a14ebb28f2068fee0891ffc9
BLAKE2b-256 61e4acfa3819b231a09e3e851c5bc56dfc8bab49cf023fedd3564fd46e1ea589

See more details on using hashes here.

Provenance

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