Open In App

How to Copy a Collection From One Database to Another in MongoDB?

Effective data management is crucial for MongoDB-dependent applications. As databases grow, the need to transfer collections between databases arises. This can be for creating development or testing environments, migrating data, or archiving historical information. MongoDB provides built-in capabilities and tools to simplify this process, offering flexibility and ease of use.

In This article, We will explore the different ways to copy collections in MongoDB, providing insights into when and how to use each method. It discusses the need for copying databases or collections, highlighting scenarios such as creating test environments, data migration, backups, and security considerations.



What Is the Need for Copying a Database or Collection?

When working with MongoDB environments, there might be many reasons to copy a database or collection:

Methods of Copying Collections in MongoDB

To copy collections in MongoDB is to make a replication of an existing collection, including the data and schema, into another database. There are several situations where this feature comes handy like creating development environment, migration of data or historical information archiving. MongoDB has two main ways of copying collections that have their own strengths and areas of application:



1. db.cloneCollection()

This function is a part of MongoDB and it performs better when it comes to copying collections within the same instance of MongoDB. It copies the source collection into a target database while duplicating everything including data and schema.

It is a built-in function that is recommended for copying same instance’s collections within the same MongoDB. It provides an effective and efficient process.

Here is the syntax:

use source_db  // Switch to the source database containing the collection to copy
db.source_collection.cloneCollection("target_collection", target_db) // Clone to target collection in target_db

Explanation:

2. Using mongodump and mongorestore

This option offers much more freedom since you can move collections among different instances of MongoDB or servers even. The first thing that happens here is that a compressed archive of the source collection is created then it is restored into the target database with the desired collection name

This method provides more flexibility, allowing you to copy collections between different MongoDB instances or servers. It involves creating a backup of the source collection and then restoring it into the target database. Here’s is the syntax:

mongodump -d source_db -c source_collection -o dump
mongorestore -d target_db dump/source_collection --collection target_collection

Explanation:

We’ll explore the advantages and considerations for each method, along with alternative options using visual tools, in the following sections of this article.

Example

The collection called “orders” holds customer order information for a database whose name is “ecommerce”. The reason behind creating another copy of this collection is to perform analysis on it later in a different database named “analytics”. We will see both db.cloneCollection() (Same Instance) and mongodump & mongorestore (Different Servers).

Method 1: Using db.cloneCollection() (Same Instance)

If we are currently connected to our MongoDB instance via the mongo shell, then switch to the e-commerce database:

use e-commerce

Run the db.cloneCollection() command that copies the orders collection into the analytics database as order_analysis:

db.orders.cloneCollection("order_analysis", analytics)

Expected Output:

{ "name" : "orders", "newName" : "order_analysis" }

This shows that the orders has been copied successfully as order_analysis into analytics.

Method 2: Mongodump and Mongorestore (Different Servers)

For example, on the source server (e-commerce database):

Then we have to create a dump of “orders” collection:

Bash
mongodump -d e-commerce -c orders -o dump

The command will generate a compressed archive called “dump” in the current directory containing the information from the “orders” collection.

On the target server (analytics database)

Once we are connected to destination server, navigate to the location where we would like to keep dump file if it is not same as source server.

To restore this dump and thus create order_history collections inside analytic database. use below code:

Bash
mongorestore -d analytics dump/orders --collection order_history

This command restores archive from directory named ‘dump’ and creates new collection named ‘order_history’ under analytical database.

Choosing The Right Method

In our case this could be done by simply using db.cloneCollection() when both e-commerce and analytic databases are located in one MongoDB instance. However, mongodump and mongorestore are essential when transferring collection data across network between two separate servers.

Conclusion

Overall, this article has explained the various methods available for copying collections in MongoDB, offering valuable insights into their application and suitability. It has explained the importance of copying databases or collections in scenarios such as creating test environments, data migration, backups, and ensuring security. By providing detailed explanations of two main methods such as db.cloneCollection() and mongodump/mongorestore about how to efficiently manage their MongoDB data.


Article Tags :