Convert JSON patches into a MongoDB update.
This is library can help converts json-patch into mongo-query, east adapt to known drivers.
from jsonpatch_to_mongodb import parse_patches
patches = [{'op': 'add', 'path': '/foo/1', 'value': 'qux'}]
parse_patches(patches) # {'$push': {'foo': {'$each': ['qux'], '$position': 1}}}
also we can add prefix
for patches and each key will be affected, useful when need update nested fields.
from jsonpatch_to_mongodb import parse_patches
patches= [{'op': 'add', 'path': '/foo/1', 'value': 'qux'}]
parse_patches(patches, "test.") # {'$push': {'test.foo': {'$each': ['qux'], '$position': 1}}}
- add
- test
- replace
- remove (issue with unset in array, don't remove element in fact, just set as null)
- copy
- move
pip install jsonpatch-to-mongodb
make test
MIT
Credits This is a Python port of the JavaScript and Golang library jsonpatch-to-mongodb.