mongo-diff
mongo-diff is a command-line tool people can use to compare two MongoDB collections.
Those collections can reside in either a single database or two separate databases (even across servers).
%% This is the source code of a Mermaid diagram, which GitHub will render as a diagram.
%% Note: PyPI does not render Mermaid diagrams, and instead displays their source code.
%% Reference: https://github.com/pypi/warehouse/issues/13083
graph LR
script[["mongo_diff.py"]]
result["List of<br>differences"]
subgraph s1 \[Server]
subgraph d1 \[Database]
collection_a[("Collection A")]
end
end
subgraph s2 \[Server]
subgraph d2 \[Database]
collection_b[("Collection B")]
end
end
collection_a --> script
collection_b --> script
script --> result
Usage
Installation
Assuming you have pipx installed, you can install the tool by running the following command:
pipx install mongo-diff
pipxis a tool people can use to download and install Python scripts that are hosted on PyPI. You can installpipxby running$ python -m pip install pipx.
Running
You can display the tool's --help snippet by running:
mongo-diff --help
At the time of this writing, the tool's --help snippet is:
Usage: mongo-diff [OPTIONS]
Compare two MongoDB collections.
Those collections can reside in either a single database or two separate
databases (even across servers).
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --include-oid,--include-id Include the `_id` field when comparing │
│ documents (`--include-id` is │
│ deprecated). │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Collection A ───────────────────────────────────────────────────────────────╮
│ * --mongo-uri-a TEXT Connection string for accessing │
│ the MongoDB server containing │
│ collection A. │
│ [env var: MONGO_URI_A] │
│ [required] │
│ * --database-name-a TEXT Name of the database containing │
│ collection A. │
│ [required] │
│ * --collection-name-a TEXT Name of collection A. [required] │
│ --identifier-field-name-a TEXT Name of the field of each document │
│ in collection A to use to identify │
│ a corresponding document in │
│ collection B. The values in this │
│ field must be unique within each │
│ collection. │
│ [default: id] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Collection B ───────────────────────────────────────────────────────────────╮
│ --mongo-uri-b TEXT Connection string for accessing the │
│ MongoDB server containing collection │
│ B (if different from that specified │
│ for collection A). │
│ [env var: MONGO_URI_B] │
│ --database-name-b TEXT Name of the database containing │
│ collection B (if different from that │
│ specified for collection A). │
│ --collection-name-b TEXT Name of collection B (if different │
│ from that specified for collection │
│ A). │
│ --identifier-field-name-b TEXT Name of the field of each document in │
│ collection B to use to identify a │
│ corresponding document in collection │
│ A (if different from that specified │
│ for collection A). The values in this │
│ field must be unique within each │
│ collection. │
╰──────────────────────────────────────────────────────────────────────────────╯
Note: The above snippet was captured from a terminal window whose width was 80 characters.
MongoDB connection strings
As documented in the --help snippet above, you can provide the MongoDB connection strings to the tool via either (a)
command-line options; or (b) environment variables named MONGO_URI_A and MONGO_URI_B. The latter can come in handy
for MongoDB connection strings that contain passwords.
Here's how you could create those environment variables:
export MONGO_URI_A='mongodb://localhost:27017'
export MONGO_URI_B='mongodb://username:password@host.example.com:22222'
Note: That will only create those environment variables in the current shell process. You can persist them by adding those same commands to your shell initialization script (e.g.
~/.bashrc,~/.zshrc).
Example output
As the tool compares the collections, it will display the differences it detects; like this:
Document differs between collections:
--- Collection A: id='x:food-00001'
+++ Collection B: id='x:food-00001'
@@ -13,7 +13,7 @@
"type": "food"
}
],
- "name": "Salmon",
+ "name": "Salmon Fillet",
"vendor": "Alaskan Foods",
"vendorCountry": "USA",
"vendorRegion": "Alaska",
Document exists in collection A only: id='x:food-00002'
Document exists in collection A only: id='x:food-00003'
Document exists in collection B only: id='x:food-00004'
When the tool finishes comparing the collections, it will display a summary of the result; like this:
Result
╭───────────────────────────────────────────┬──────────╮
│ Description │ Quantity │
├───────────────────────────────────────────┼──────────┤
│ Documents in collection A │ 4 │
│ Documents in collection B │ 3 │
├───────────────────────────────────────────┼──────────┤
│ Documents in collection A only │ 2 │
│ Documents in collection B only │ 1 │
├───────────────────────────────────────────┼──────────┤
│ Documents that differ between collections │ 1 │
╰───────────────────────────────────────────┴──────────╯
Updating
You can update the tool to the latest version available on PyPI by running:
pipx upgrade mongo-diff
Uninstallation
You can uninstall the tool from your computer by running:
pipx uninstall mongo-diff
Development
We use uv to both (a) manage dependencies and (b) build and publish packages to PyPI.
pyproject.toml: Configuration file foruvand other toolsuv.lock: Lockfile listing resolved dependencies, direct and indirect (generated via$ uv lock)
Clone repository
git clone https://github.com/eecavanna/mongo-diff.git
cd mongo-diff
Set up virtual environment and install dependencies
Create the .venv virtual environment and install the project's dependencies (including those in the dev group):
uv sync
You can run commands inside the virtual environment using
$ uv run <command>without activating it, or activate it directly via$ source .venv/bin/activate(and detach via$ deactivate).
Make changes
Edit the tool's source code and documentation however you want.
While editing the tool's source code, you can run the tool via uv run in order to test things out:
uv run mongo-diff --help
Run tests
We currently only have a smattering of doctests in this codebase. You can run them using pytest (which is configured in pyproject.toml to run doctests automatically) via:
uv run pytest
We may eventually populate the tests/ directory with a more exhaustive test suite,
using pytest and mongomock.
Build package
Update package version
PyPI doesn't allow people to publish the same "version" of a package multiple times.
You can update the version identifier of the package by running:
uv version {version}
Or bump a version component with:
uv version --bump {keyword}
You can replace
{version}with a literal version identifier (e.g.0.3.2) or{keyword}with a bump keyword (e.g.major,minor, orpatch). You can run$ uv version --helpto see all options.
Alternatively, you can manually edit the version line in pyproject.toml (and run $ uv lock to update uv.lock):
- version = "0.1.0"
+ version = "0.1.1"
Build the package
Build the package based upon the latest source code:
uv build
That will create both a source distribution file (whose name ends with
.tar.gz) and a wheel file (whose name ends with.whl) in thedistdirectory.
Publish package
Set up PyPI credentials
Create the following
environment variable,
which uv will check for if credentials aren't specified via CLI options:
export UV_PUBLISH_TOKEN="{api_token}"
Replace
{api_token}with a PyPI API token whose scope includes the PyPI project to which you want to publish the package.
Publish package to PyPI
Publish the newly-built package to PyPI:
uv publish
At this point, people will be able to download and install the package from PyPI.
Metadata
Release files for mongo-diff 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mongo_diff-0.4.0.tar.gz | 10.1 kB | Without provenance | Details for mongo_diff-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mongo_diff-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for mongo_diff-0.4.0-py3-none-any.whl |
Total release size: 20.7 kB
Release files / mongo_diff-0.4.0.tar.gz
| Download URL | mongo_diff-0.4.0.tar.gz |
|---|---|
| Size | 10.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
baee0ea0fb20cdb90e78ea221ed4e73b7b9e20d7dd8d6f6c94b809aef0e00e54
|
|
BLAKE2b-256 checksum How to use checksums |
7eb07946c97ae732ed2e43702808646281651ce5455ec6ee537692dbc5085429
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.4 {"installer":{"name":"uv","version":"0.12.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / mongo_diff-0.4.0-py3-none-any.whl
| Download URL | mongo_diff-0.4.0-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1edd2ea81607db53651a3b16d91fd278cfdb20bb4677beb2616c1f6f5b90ed7e
|
|
BLAKE2b-256 checksum How to use checksums |
8a2804fb99d8b789a80ec7255e98085a7495ffa8387a512a08411a3625864963
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.4 {"installer":{"name":"uv","version":"0.12.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for mongo-diff 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.