Copying a collection in the same database in MongoDB
Duplicate a collection in the same database is an amazing task. It saves time and effort. This tutorial demonstrates how to replicate a collection in the same database using MongoDB mongodump
, mongorestore
, aggregate()
and .forEach()
Copying collections in the same database when working in MongoDB
In older versions of MongoDB, like less than 3.0, we could use copyTo()
method as to db.collection_name.copyTo()
copy a collection, but it is deprecated now.
eval
It is also deprecated starting with MongoDB version 4.2. Note that db.collection_name.copyTo()
it is wrapped eval
, which means we cannot use any of them to copy collections if we have MongoDB 4.2 or higher.
There are other ways to replicate collections in the same database in newer versions of MongoDB. Some of them are listed here.
Let’s start learning them one by one.
To replicate a collection in the same database in MongoDB mongodump
,
usemongorestore
This is the quickest way to clone/duplicate a collection in the same database using the MongoDB Database Tools; specifically, we can use mongodump
and mongorestore
. The Database Tools are a suite of command-line utilities for working in MongoDB.
We can use the following command in the Windows command prompt to check the versions of mongodump
and mongorestore
. If it successfully returns the corresponding version, the database tools are installed.
Otherwise, follow this to install the database tools.
Sample code:
C:/Users/Dell> mongodump --version
Sample code:
C:/Users/Dell> mongorestore --version
Remember, we have to execute the mongodump
and mongorestore
commands from the command line of the system, for example, from the command prompt in Windows OS or terminal if we are using Ubuntu. Never run this command from the mongo shell.
Once we have the database tools for MongoDB, run the following command to teachers
dump the collection into the same database: 'test`.
Sample code:
C:/Users/Dell> mongodump -d test -c teachers
Output:
2022-05-27T13:05:14.497+0500 writing test.teachers to dump\test\teachers.bson
2022-05-27T13:05:14.503+0500 done dumping test.teachers (3 documents)
The above output indicates that the dump file is written dump\test\teachers.bson
. So, we need to restore it using the command given below.
Sample code:
C:/Users/Dell>mongorestore -d test -c teachers1 --dir=dump/<db>/<sourcecollection.bson>
Output:
2022-05-27T13:05:28.085+0500 checking for collection data in dump\test\teachers.bson
2022-05-27T13:05:28.088+0500 reading metadata for test.teachers1 from dump\test\teachers.metadata.json
2022-05-27T13:05:28.252+0500 restoring test.teachers1 from dump\test\teachers.bson
2022-05-27T13:05:28.312+0500 finished restoring test.teachers1 (3 documents, 0 failures)
2022-05-27T13:05:28.312+0500 no indexes to restore for collection test.teachers1
2022-05-27T13:05:28.313+0500 3 document(s) restored successfully. 0 document(s) failed to restore.
The output will be as shown above, which means the collection has been copied successfully. If the target collection named does not exist in the current database teachers1
, it will be created.
Next, open the mongo shell and execute the following query to see if the replicated collection exists.
Sample code:
> show collections
Output:
teachers
teachers1
Alternatively, we can also export a collection from mongoexport
a database using and then import it into a collection in the same database using .test
teachers
mongoimport
teachers2
Sample code:
C:/Users/Dell> mongoexport -d test -c teachers | mongoimport -d test -c teachers2 --drop
aggregate()
To copy a collection in the same database in MongoDB,
use the
Sample code:
> db.teachers.aggregate([{$out: "teachers3"}])
This command is executed using the mongo shell. Afterwards, we use show collections
the command to see teachers3
if the exists.
Sample code:
> show collections
Output:
teachers
teachers1
teachers2
teachers3
We are using aggregation pipeline to return data from teachers
collection and write them to the specified collection, here teachers3
. We can use this approach if we have MongoDB 4.4 or higher.
Use forEach()
loop to copy collections in the same database in MongoDB
Sample code:
> db.teachers.find().forEach((doc) => {
db.teachers4.insert(doc)
})
Use the following query to confirm teachers4
that is in the same database.
Sample code:
> show collections
Output:
teachers
teachers1
teachers2
teachers3
teachers4
This method is the slowest compared to all the above methods due to the use of loop. It iterates over all the documents of the source collection and inserts them one by one into the target collection.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
List all collections in MongoDB Shell
Publish Date:2025/04/29 Views:156 Category:MongoDB
-
When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database. These methods are as follows: show collections List all c
Querying for non-null values in MongoDB
Publish Date:2025/04/29 Views:139 Category:MongoDB
-
This MongoDB article will explain how to query for non-null values in MongoDB. To query for non-null values, you can use $ne the operator and $eq the operator and then specify the desired value to query. This article shows the readers
Shutting down with code:100 error in MongoDB
Publish Date:2025/04/29 Views:53 Category:MongoDB
-
This MongoDB tutorial will teach you to fix the error on different operating systems shutting down with code:100 . It will also talk about the root cause of why this issue occurs. shutting down with code:100 Errors in MongoDB As we all know
SELECT COUNT GROUP BY in MongoDB
Publish Date:2025/04/29 Views:74 Category:MongoDB
-
In this article, we will discuss the functions in MongoDB. Also, we will point out the aggregation functions in detail. We will explain in detail the different ways to count and sort multiple and single fields of Group in MongoDB. Operation
Differences between MongoDB and Mongoose
Publish Date:2025/04/29 Views:80 Category:MongoDB
-
This MongoDB article will discuss the differences between MongoDB and Mongoose. Unfortunately, most beginners tend to confuse these two concepts when they start developing applications and use MongoDB as their backend. MongoDB has its own s
Install MongoDB using Homebrew
Publish Date:2025/04/29 Views:161 Category:MongoDB
-
MongoDB is a well-known unstructured database management system that can handle large amounts of data. It is a document-oriented database system and belongs to the NoSQL family (non-SQL). Data and records are stored as documents that look a
Create a MongoDB dump of the database
Publish Date:2025/04/29 Views:62 Category:MongoDB
-
In this MongoDB article, you’ll get a walkthrough of Mongodump and Mongorestore , how to use them, and some simple examples of backing up and restoring your collections using both tools. mongodump Commands in MongoDB Mongodump is a tool t
Get the size of the database in MongoDB
Publish Date:2025/04/29 Views:88 Category:MongoDB
-
When working in MongoDB, do you know the size of your database? Today, we will learn how to get the size of a database in MongoDB using show dbs the command and the method. db.stats() Get the size of the database in MongoDB We can show dbs;
Grouping values by multiple fields with MongoDB
Publish Date:2025/04/29 Views:99 Category:MongoDB
-
MongoDB Group by Multiple Fields is used to group values by multiple fields using various methods. One of the most efficient ways to group various fields present in MongoDB documents is by using $group the operator, which helps in per