Convert JSON patch into pymongo
Project description
jsonpatch2pymongo
Introduction
- This is a port of jsonpatch to mongodb and jsonpatch to mongodb - golang into python language.
- Anyone who want to apply differences between 2 JSON data into mongodb using pymongo's update_many() function will be beneficial from this.
How to use
First, you need to get patch list generated by jsonpatch package.
>>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
>>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
>>> patch = jsonpatch.JsonPatch.from_diff(src, dst)
# or equivalently
>>> patch = jsonpatch.make_patch(src, dst)
>>> patch.patch
[{'op': 'remove', 'path': '/foo'}, {'op': 'add', 'path': '/baz', 'value': 'qux'}, {'op': 'remove', 'path': '/numbers/1'}, {'op': 'add', 'path': '/numbers/2', 'value': 7}, {'op': 'remove', 'path': '/numbers/3'}]
Once patch(list of each patch) is available, you can pass it to this function
>>> update = jsonpatch2pymongo(patch.patch)
>>> update
{'$set': {'baz': 'qux'}, '$unset': {'foo': 1, 'numbers.1': 1, 'numbers.3': 1}, '$push': {'numbers': {'$each': [7], '$position': 2}}}
And finally, you can pass update
into pymongo's update or update_many function
db.collection.update_many(filter, update)
Testcases
- Install pytest package and run
python -m pytest -v
under root directory. - Then pytest will automatically run test_main.py which includes various test cases ported from the original javascript test cases.
Code style
black -l 100
Setup pre-commit hook
- Plugins specified in
.pre-commit-config.yml
will be used before you commit changes. - How to enable pre-commit hook
pip install pre-commit
pre-commit install
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for jsonpatch2pymongo-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 441edb0c4fb449fa8102aea027c17c5775e104b025a7f27bdf9e68600c02d19e |
|
MD5 | bec44630d872b620e336bf1e4374a11a |
|
BLAKE2b-256 | 05af822d13401d3ddbc2bbbda689c2ede483162792c029214ba646acb2a435cd |