
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Clone a collection in MongoDB?
To clone a collection in MongoDB, you can use the forEach() method. Let us first create a collection with a document.
The query to create a collection with a document is as follows −
> db.studentInformation.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bc15780f10143d8431e21") } > db.studentInformation.insertOne({"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bc15e80f10143d8431e22") } > db.studentInformation.insertOne({"StudentName":"James"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bc17380f10143d8431e23") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.studentInformation.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8bc15780f10143d8431e21"), "StudentName" : "Chris" } { "_id" : ObjectId("5c8bc15e80f10143d8431e22"), "StudentName" : "Robert" } { "_id" : ObjectId("5c8bc17380f10143d8431e23"), "StudentName" : "James" }
Here is the query to make a clone in MongoDB −
> db.studentInformation.find().forEach( function(copyValue){db.makingStudentInformationClone.insert(copyValue)} );
Let us check the documents of clone collection in MongoDB. The query is as follows −
> db.makingStudentInformationClone.find();
The following is the output −
{ "_id" : ObjectId("5c8bc15780f10143d8431e21"), "StudentName" : "Chris" } { "_id" : ObjectId("5c8bc15e80f10143d8431e22"), "StudentName" : "Robert" } { "_id" : ObjectId("5c8bc17380f10143d8431e23"), "StudentName" : "James" }
Let us now check the list of all collections including clone. The query is as follows −
> show collections;
The following is the output −
copyThisCollectionToSampleDatabaseDemo deleteDocuments deleteDocumentsDemo deleteSomeInformation employee getElementWithMaxIdDemo internalArraySizeDemo makingStudentInformationClone prettyDemo selectWhereInDemo sourceCollection studentInformation updateInformation userInformation
- Related Articles
- Create index in a MongoDB collection?
- Renaming column name in a MongoDB collection?
- How to drop a collection in MongoDB?
- Group by dates in a MongoDB collection?
- Change collection name in MongoDB?
- Retrieve data from a MongoDB collection?
- MongoDB query to rename a collection?
- Retrieving the first document in a MongoDB collection?
- How can I rename a collection in MongoDB?
- Get execution stats in MongoDB for a collection
- How to add a column in MongoDB collection?
- How to create a new collection in MongoDB?
- Get all fields names in a MongoDB collection?
- Project specific array field in a MongoDB collection?
- Search array of objects in a MongoDB collection?

Advertisements