Skip to main content

Code style: black

jsonpatch2pymongo

Introduction

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

Release files for jsonpatch2pymongo 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonpatch2pymongo 0.1.3
File Size Uploaded
jsonpatch2pymongo-0.1.3.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonpatch2pymongo 0.1.3
File Interpreter ABI Platform
jsonpatch2pymongo-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 9.2 kB

Release files / jsonpatch2pymongo-0.1.3.tar.gz

Download URL jsonpatch2pymongo-0.1.3.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
363943a595e434c727cbb7f631ead83d8cedb8da95b6e5ef2cef2f6007490da6
BLAKE2b-256 checksum
How to use checksums
9fd90a24749ac69b61a2df61c675a6655a31ab9e61070d445845a123d25e29fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / jsonpatch2pymongo-0.1.3-py3-none-any.whl

Download URL jsonpatch2pymongo-0.1.3-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
554e02c069e4984793138ee7521a6c53438af4b8b6247c6b821b141d955f30dd
BLAKE2b-256 checksum
How to use checksums
10f64fe3a3d3a89737765d3aaa4b1fad8ab035a1547b8ef39b8f77aed379f10d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

1 release file

0.1.1

2 release 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