Skip to main content

mongodol

MongoDB Data Object Layer.

Tools to create data abstractions over mongoDB data.

To install: pip install mongodol

And of course, you need to install MongoDB

The base objects

from mongodol import (
    MongoClientReader,
    MongoDbReader,
    MongoCollectionReaderBase,
    MongoCollectionReader,
    MongoCollectionPersister,
)

MongoClientReader gives you access to the databases for a mongoDB host (default is localhost). The keys are database names...

client = MongoClientReader()
list(client)
['admin', 'config', 'local', 'py2store', 'py2store_tests', 'yf']

... and the values are db objects. The keys of db objects are collection names...

db = client["py2store"]
list(db)
['tmp', 'test', 'annots_example']

... and the values are collection objects.

mgc = db["test"]
len(mgc)
0

The collection is empty. Let's get a collection object that we can actually write with.

Here, we show how you can write by appending data:

writable_mgc = MongoCollectionPersister(mgc)
writable_mgc.append({"mongo": "uses", "json": "data"})
<pymongo.results.InsertOneResult at 0x1203f9940>

See that we have data in the collection now:

keys = list(mgc)
keys
[{'_id': ObjectId('60359a2993b7670664918663')}]

But that's just showing the key, let's see the value under that key:

k = keys[0]
mgc[k]
<pymongo.cursor.Cursor at 0x120fc1be0>

Oh... you get a cursor back. It's okay, a cursor is the object that will provide you with the data you requested if and when you want it.

Let's say you want it now. Just "consume" the cursor. If you're expecting just one item under that key, do this:

v = next(
    mgc[k], None
)  # the None is there as a sentinel -- it will be used to indicate if mgc[k] has no data for you.
v
{'mongo': 'uses', 'json': 'data'}

So indeed it worked.

You can also use extend to write in bulk.

writable_mgc.extend(
    [
        {"kind": "example", "data": 2},
        {"kind": "example", "data": [1, 2, 3]},
        {"kind": "example", "data": {"nested": "dict"}},
    ]
)
<pymongo.results.InsertManyResult at 0x11e520200>
list(mgc)
[{'_id': ObjectId('60359a2993b7670664918663')},
 {'_id': ObjectId('60359ac193b7670664918664')},
 {'_id': ObjectId('60359ac193b7670664918665')},
 {'_id': ObjectId('60359ac193b7670664918666')}]

So far, MongoDB gave us an id. MongoDB will make it's own id if we don't ask for a particular one.

But you can also write data to a key of your choice. With the base persister which we're demoing now, with it's base defaults, you need to specify your key as a {'_id': YOUR_CHOICE_OF_ID}.

writable_mgc[{"_id": "my_id"}] = {"my": "data"}
list(mgc)
[{'_id': ObjectId('60359a2993b7670664918663')},
 {'_id': ObjectId('60359ac193b7670664918664')},
 {'_id': ObjectId('60359ac193b7670664918665')},
 {'_id': ObjectId('60359ac193b7670664918666')},
 {'_id': 'my_id'}]
mgc[{"_id": "my_id"}]
{'my': 'data'}

You can delete data given a key:

del writable_mgc[{"_id": "my_id"}]
list(mgc)
[{'_id': ObjectId('60359a2993b7670664918663')},
 {'_id': ObjectId('60359ac193b7670664918664')},
 {'_id': ObjectId('60359ac193b7670664918665')},
 {'_id': ObjectId('60359ac193b7670664918666')}]

So far, we've seen the base classes.

So far, you have no reason what-so-ever to use mongodol. Might as well use pymongo (which it wraps) directly.

The real reason for using mongodol is that it is a gateway to enabling all the py2store goodies to create the key-value perspectives that make sense to you, without all the backend-dependent boilerplate over the business logic.

So let's show one example of how to do that.

The real reason you want to use mongodol (an example)

Let's say we have the collection we just made above, but

  • We want to access data by doing s['60359a2993b7670664918663'] instead of the (annoying) s[{'_id': ObjectId('60359a2993b7670664918663')}]
  • We'd like our values to to come in the form of actual ready to use data. Namely, we want to automatically ask the cursor for it's first element (assuming it's unique for that key), and we'd like to extract the 'data' field from that result.
  • We'd like to peruse only part of the mongo collection; only if there's a 'kind' field and it's equal to 'example'.

Here's how it can be done:

from bson import ObjectId
from py2store import wrap_kvs
from mongodol import MongoCollectionReaderBase


@wrap_kvs(
    id_of_key=lambda x: {"_id": ObjectId(x)},
    key_of_id=lambda x: str(x["_id"]),
    obj_of_data=lambda doc: next(doc, None)["data"],
)
class MyStore(MongoCollectionReaderBase):
    """my special store"""
s = MyStore(
    mgc=mgc, key_fields=("_id",), data_fields=("data",), filt={"kind": "example"}
)
list(s)
['60359ac193b7670664918664',
 '60359ac193b7670664918665',
 '60359ac193b7670664918666']
s["60359ac193b7670664918664"]
2
list(s.values())
[2, [1, 2, 3], {'nested': 'dict'}]

Download files

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

Source Distribution

mongodol-0.1.3.tar.gz (45.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mongodol-0.1.3-py3-none-any.whl (52.3 kB view details)

Uploaded Python 3

File details

Details for the file mongodol-0.1.3.tar.gz.

File metadata

  • Download URL: mongodol-0.1.3.tar.gz
  • Upload date:
  • Size: 45.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mongodol-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1dd8a368c3cb640669e973f5aa510213ac1893c28b766a710807ecf90b496184
MD5 e995e52700ed7868fc93dbd8954d078f
BLAKE2b-256 98506d84449ff3f0a37f43cc2df35725a5098caa15a7cd183f641758d6522ae9

See more details on using hashes here.

File details

Details for the file mongodol-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: mongodol-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 52.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for mongodol-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 279b947e5f7ec1dda2f7244d5a4b97e18a3bc502e4cdfdc08ea1418e8111b584
MD5 8c0fb3f913342f9fb9b74f4015f5db6c
BLAKE2b-256 f09b6b2f3d48a75a2ec9b2031e6aff7abd6ef5dda3a90493293cda4c1eadb8f4

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.5

2 files

0.1.4

2 files

This release

0.1.3 This release

2 files

0.1.2

1 file

0.1.1

1 file

0.1.0

1 file

0.0.26

1 file

0.0.25

1 file

0.0.24

1 file

0.0.23

1 file

0.0.22

1 file

0.0.21

1 file

0.0.20

1 file

0.0.19

1 file

0.0.18

1 file

0.0.17

1 file

0.0.16

1 file

0.0.15

1 file

0.0.14

1 file

0.0.13

1 file

0.0.12

1 file

0.0.11

1 file

0.0.10

1 file

0.0.9

1 file

0.0.8

1 file

0.0.7

1 file

0.0.6

1 file

0.0.5

1 file

0.0.4

1 file

0.0.3

2 files

0.0.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page