jaraco-mongodb 12.4.0


pip install jaraco-mongodb

  Latest version

Released: Apr 22, 2025

Project Links

Meta
Author: Jason R. Coombs
Requires Python: >=3.9

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers

Programming Language
  • Python :: 3
  • Python :: 3 :: Only

Framework
  • Pytest
https://img.shields.io/pypi/v/jaraco.mongodb.svg https://img.shields.io/pypi/pyversions/jaraco.mongodb.svg tests Ruff https://readthedocs.org/projects/jaracomongodb/badge/?version=latest https://img.shields.io/badge/skeleton-2025-informational

Migration Manager

jaraco.mongodb.migration implements the Migration Manager as featured at the MongoWorld 2016 presentation From the Polls to the Trolls. Use it to load documents of various schema versions into a target version that your application expects.

install

jaraco.mongodb.install makes it super easy to get a download of the community version of MongoDB on any platform:

py -m jaraco.mongodb.install

By default, it will install to the current directory. Use --target to install to another location.

sessions

jaraco.mongodb.sessions implements a CherryPy Sessions store backed by MongoDB.

By default, the session store will handle sessions with any objects that can be inserted into a MongoDB collection naturally.

To support richer objects, one may configure the codec to use jaraco.modb.

fields

jaraco.mongodb.fields provides two functions, encode and decode, which take arbitrary unicode text and transform it into values suitable as keys on older versions of MongoDB by backslash-escaping the values.

monitor-index-creation

To monitor an ongoing index operation in a server, simply invoke:

python -m jaraco.mongodb.monitor-index-creation mongodb://host/db

move-gridfs

To move files from one gridfs collection to another, invoke:

python -m jaraco.mongodb.move-gridfs –help

And follow the usage for moving all or some gridfs files and optionally deleting the files after.

oplog

This package provides an oplog module, which is based on the mongooplog-alt project, which itself is a Python remake of official mongooplog utility, shipped with MongoDB starting from version 2.2 and deprecated in 3.2. It reads oplog of a remote server, and applies operations to the local server. This can be used to keep independed replica set loosly synced in much the same way as Replica Sets are synced, and may be useful in various backup and migration scenarios.

oplog implements basic functionality of the official utility and adds following features:

  • tailable oplog reader: runs forever polling new oplog event which is extremly useful for keeping two independent replica sets in almost real-time sync.

  • option to sync only selected databases/collections.

  • option to exclude one or more namespaces (i.e. dbs or collections) from being synced.

  • ability to “rename” dbs/collections on fly, i.e. destination namespaces can differ from the original ones. This feature works on mongodb 1.8 and later. Official utility only supports version 2.2.x and higher.

  • save last processed timestamp to file, resume from saved point later.

Invoke the command as a module script: python -m jaraco.mongodb.oplog.

Command-line options

Usage is as follows:

$ python -m jaraco.mongodb.oplog  --help
usage: oplog.py [--help] [--source host[:port]] [--oplogns OPLOGNS]
                [--dest host[:port]] [-w WINDOW] [-f] [--ns [NS [NS ...]]]
                [-x [EXCLUDE [EXCLUDE ...]]]
                [--rename [ns_old=ns_new [ns_old=ns_new ...]]] [--dry-run]
                [--resume-file FILENAME] [-s SECONDS] [-l LOG_LEVEL]

optional arguments:
  --help                show usage information
  --source host[:port]  Hostname of the mongod server from which oplog
                        operations are going to be pulled. Called "--from" in
                        mongooplog.
  --oplogns OPLOGNS     Source namespace for oplog
  --dest host[:port]    Hostname of the mongod server (or replica set as <set
                        name>/s1,s2) to which oplog operations are going to be
                        applied. Default is "localhost". Called "--host" in
                        mongooplog.
  -w WINDOW, --window WINDOW
                        Time window to query, like "3 days" or "24:00" (24
                        hours, 0 minutes).
  -f, --follow          Wait for new data in oplog. Makes the utility polling
                        oplog forever (until interrupted). New data is going
                        to be applied immediately with at most one second
                        delay.
  --ns [NS [NS ...]]    Process only these namespaces, ignoring all others.
                        Space separated list of strings in form of ``dname``
                        or ``dbname.collection``. May be specified multiple
                        times.
  -x [EXCLUDE [EXCLUDE ...]], --exclude [EXCLUDE [EXCLUDE ...]]
                        List of space separated namespaces which should be
                        ignored. Can be in form of ``dname`` or
                        ``dbname.collection``. May be specified multiple
                        times.
  --rename [ns_old=ns_new [ns_old=ns_new ...]]
                        Rename database(s) and/or collection(s). Operations on
                        namespace ``ns_old`` from the source server will be
                        applied to namespace ``ns_new`` on the destination
                        server. May be specified multiple times.
  --dry-run             Suppress application of ops.
  --resume-file FILENAME
                        Read from and write to this file the last processed
                        timestamp.
  -l LOG_LEVEL, --log-level LOG_LEVEL
                        Set log level (DEBUG, INFO, WARNING, ERROR)

Example usages

Consider the following sample usage:

python -m jaraco.mongodb.oplog --source prod.example.com:28000 --dest dev.example.com:28500 -f --exclude logdb data.transactions --seconds 600

This command is going to take operations from the last 10 minutes from prod, and apply them to dev. Database logdb and collection transactions of data database will be omitted. After operations for the last minutes will be applied, command will wait for new changes to come, keep running until Ctrl+C or other termination signal recieved.

The tool provides a --dry-run option and when logging at the DEBUG level will emit the oplog entries. Combine these to use the tool as an oplog cat tool:

$ python -m jaraco.mongodb.oplog --dry-run -s 0 -f --source prod.example.com --ns survey_tabs -l DEBUG

Testing

Tests for oplog are written in javascript using test harness which is used for testing MongoDB iteself. You can run the oplog suite with:

mongo tests/oplog.js

Tests produce alot of output. Succesful execution ends with line like this:

ReplSetTest stopSet *** Shut down repl set - test worked ****

These tests are run as part of the continuous integration and release acceptance tests in Travis.

Fixtures

jaraco.mongodb provides some pretty sophisticated pytest fixtures.

mongodb_instance is a running MongoDB instance with a PyMongo connection ready to use. It attempts to locate an existing MongoDB install, or if it cannot find one, it downloads the community edition and installs it to a temporary directory for the test session.

This fixture makes it straightforward to run scripts like this example in pip-run that can fully validate an expectation about MongoDB behavior on any platform without any dependency but pip-run (and of course Python).

12.4.0 Apr 22, 2025
12.3.1 Apr 12, 2025
12.3.0 Apr 12, 2025
12.2.0 Apr 12, 2025
12.1.3 Apr 12, 2025
12.1.2 Sep 01, 2024
12.1.1 Aug 20, 2024
12.1.0 Apr 25, 2024
12.0.0 Sep 01, 2023
11.5.1 Sep 01, 2023
11.5.0 Aug 31, 2023
11.4.1 Aug 31, 2023
11.4.0 Jul 29, 2023
11.3.0 Jul 29, 2023
11.2.1 Jul 28, 2021
11.2.0 May 01, 2021
11.1.0 Apr 30, 2021
11.0.1 Apr 30, 2021
11.0.0 Nov 21, 2020
10.3.0 Apr 18, 2019
10.2.0 Apr 16, 2019
10.1.3 Apr 15, 2019
10.1.2 Feb 28, 2019
10.1.1 Feb 27, 2019
10.1 Jan 12, 2019
10.0 Jan 01, 2019
9.4 Dec 14, 2018
9.4b5 Dec 13, 2018
9.3 Dec 11, 2018
9.2.1 Nov 14, 2018
9.2 Nov 14, 2018
9.1 Nov 12, 2018
9.0 Oct 29, 2018
8.1 Oct 26, 2018
8.0 Oct 06, 2018
7.10 Sep 14, 2018
7.9 Aug 21, 2018
7.7 Jul 31, 2018
7.6 Jul 10, 2018
7.5 Feb 13, 2018
7.3.1 Jan 11, 2018
7.2.3 Jan 10, 2018
7.2.1 Nov 15, 2017
7.1 Jul 12, 2017
7.0.2 Jul 11, 2017
7.0 Jun 24, 2017
6.3.1 May 01, 2017
6.3 Apr 28, 2017
6.2.1 Apr 27, 2017
6.2 Apr 27, 2017
6.1.1 Mar 30, 2017
6.1 Mar 30, 2017
5.6 Feb 07, 2017
5.5 Dec 19, 2016
5.4 Sep 28, 2016
5.3 Sep 21, 2016
5.2.1 Aug 16, 2016
5.2 Aug 16, 2016
5.1.1 Aug 16, 2016
5.1 Aug 16, 2016
5.0 Aug 15, 2016
4.4 Aug 12, 2016
4.3.1 Aug 10, 2016
4.3 Aug 10, 2016
4.2.2 Aug 04, 2016
4.2.1 Aug 02, 2016
4.2 Aug 02, 2016
4.1 Jul 29, 2016
4.0 Jul 21, 2016
3.18.1 Jul 21, 2016
3.18 Jun 23, 2016
3.17 Jun 21, 2016
3.16 Jun 20, 2016
3.15.2 May 11, 2016
3.15.1 May 11, 2016
3.15 May 11, 2016
3.14 Mar 07, 2016
3.13 Mar 07, 2016
3.12 Dec 21, 2015
3.11 Dec 04, 2015
3.10 Dec 02, 2015
3.9.4 Nov 13, 2015
3.9.3 Nov 13, 2015
3.9.2 Nov 13, 2015
3.9.1 Nov 13, 2015
3.9 Nov 13, 2015
3.8 Nov 11, 2015
3.7.1 Nov 11, 2015
3.7 Nov 11, 2015
3.6.1 Nov 10, 2015
3.6 Nov 10, 2015
3.5 Nov 10, 2015
3.4.2 Nov 04, 2015
3.4.1 Nov 04, 2015
3.4 Nov 04, 2015
3.3.2 Nov 04, 2015
3.3.1 Nov 03, 2015
3.3 Nov 03, 2015
3.2 Nov 02, 2015
3.1 Nov 02, 2015
3.0 Oct 31, 2015
2.9 Oct 31, 2015
2.8.1 Oct 31, 2015
2.8 Oct 29, 2015
2.7 Jul 23, 2015
2.6 Apr 25, 2015
2.5 Feb 17, 2015
2.4.1 Feb 13, 2015
2.4 Feb 13, 2015
2.3 Feb 10, 2015
2.2 Sep 05, 2014
2.1 Jun 13, 2014
2.0 Jan 26, 2014
1.0 Jan 26, 2014
0.0.0 Dec 11, 2018

Wheel compatibility matrix

Platform Python 3
any

Files in release