https://github.com/joegasewicz/mongo-id-marshaller
Small Python library to marshal mongo ObjectId's
https://github.com/joegasewicz/mongo-id-marshaller
Last synced: 6 months ago
JSON representation
Small Python library to marshal mongo ObjectId's
- Host: GitHub
- URL: https://github.com/joegasewicz/mongo-id-marshaller
- Owner: joegasewicz
- Created: 2020-09-24T11:59:46.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-24T14:50:40.000Z (about 5 years ago)
- Last Synced: 2025-04-06T01:04:16.000Z (7 months ago)
- Language: Python
- Size: 7.81 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

# Mongo ID Marshaller
A small library that marshals Mongo ObjectIds## Install
```bash
pip install mongo_id_marshaller
```## Api
### `single()`
By default, the object id key is always set to `id`. you can set your own key using the `id_key` kwarg.```python
from mongo_id_marshaller import MongoId"""
mongo_data:
{
{
"_id": {$oid: "56c4b2f97c82251d12547b74"},
"longitude" : 0.81315,
"latitude" : 52.1642,
"country" : "England",
"county" : "London",
"town" : "London",
}
"""mongo_data = db.getCollection('towns').findOne({"town": "London"})
mongo_id = MongoId()
mongo_data = mongo_id.single(mongo_data)
"""
mongo_data:
{
"id" : "56c4b2f97c82251d12547b74",
"longitude" : 0.81315,
"latitude" : 52.1642,
"country" : "England",
"county" : "London",
"town" : "London",
}
"""
```
### `multiple()`The multiple method uses a Python generator function so it can handle very large
Mongo collections.Here we are setting the `id_key` to `_id`
```python
from mongo_id_marshaller import MongoId"""
mongo_data:
[
{_id: {$oid: "56c4b2f97c82251d12547b74"}},
{_id: {$oid: "56c4b2f97c82251d12547b73"}}
]
"""mongo_data = db.getCollection('towns').findOne({"town": "London"})
mongo_id = MongoId(id_key="_id")
mongo_data = mongo_id.multiple(mongo_data)
"""
mongo_data:
[
{_id: "56c4b2f97c82251d12547b74"},
{_id: "56c4b2f9s7c82251d12547b73"}
]
"""