Databases 8 min read

Migrating Data from MongoDB to Apache CouchDB Using a Simple Python Script

This guide walks through migrating documents from a MongoDB 3.4.1 cluster to an Apache CouchDB 2.2 cluster using a Python script that leverages pymongo and CouchDB’s REST API, covering environment setup, migration steps, code execution, and verification of transferred data.

Architects Research Society
Architects Research Society
Architects Research Society
Migrating Data from MongoDB to Apache CouchDB Using a Simple Python Script

This article provides a step‑by‑step tutorial for moving data from a MongoDB 3.4.1 three‑node cluster to an Apache CouchDB 2.2 three‑node cluster using a lightweight Python script.

Migration Environment

The migration works in any environment—containers, virtual machines, or bare‑metal servers. The example architecture includes three components:

Source database: MongoDB 3.4.1 cluster (three nodes) with multiple databases, collections, and documents stored on an NFS share.

Target database: CouchDB 2.2 cluster (three nodes) that initially contains only the default system databases. New databases will be created on‑the‑fly during migration.

Migration system: An Ubuntu machine equipped with pymongo 3.4.0 , Python 3.x, and the requests library. This host runs the migration script and can communicate with both MongoDB and CouchDB endpoints.

Migration Method

Start a MongoClient session to the MongoDB endpoint using pymongo.

List all databases present in MongoDB.

For each database, list all collections.

Iterate over each collection and copy documents one by one.

Establish a REST API connection to CouchDB using its service URL and required headers.

Connect to a CouchDB database with the same name as the MongoDB database; if the database does not exist, create it and insert the first copied document, then continue copying the remaining documents.

This approach is suitable for relatively small databases.

Pre‑migration considerations

MongoDB stores documents inside collections, whereas CouchDB stores documents directly in databases. In this example the script appends the collection name to the CouchDB database name (e.g., SalesDB‑Atlanta ) to preserve logical grouping, but you may choose to keep the original database names if they are unique.

Note: The example does not support migrating documents that contain attachments.

Required dependencies

pymongo version 3.4.0
python 3.x

Running the migration script

Copy the provided mongo2couch.py script to the migration system and execute it with the appropriate endpoints:

$ python mongo2couch.py -c 'http://admin:password@testcouchdb:5984' -m 'mongodb://localhost:27017'

Script behavior

The script first tests connectivity to both MongoDB and CouchDB. It then reads every database (excluding the internal admin and local databases), enumerates each collection, and copies each document to the corresponding CouchDB database. When a document is inserted for the first time, the script creates the target database automatically. Errors that are not JSON‑serializable are skipped, and a migration summary is printed at the end.

Verification

After the script finishes, you can log into the CouchDB web dashboard to confirm that the new databases have been created and that the documents have been successfully migrated.

Conclusion

The guide aims to be easy to follow and help you successfully migrate your MongoDB documents to CouchDB.

data migrationpythonDatabaseMongoDBNoSQLCouchDB
Architects Research Society
Written by

Architects Research Society

A daily treasure trove for architects, expanding your view and depth. We share enterprise, business, application, data, technology, and security architecture, discuss frameworks, planning, governance, standards, and implementation, and explore emerging styles such as microservices, event‑driven, micro‑frontend, big data, data warehousing, IoT, and AI architecture.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.