MongoDB में संग्रह को क्लोन करने के लिए, आप forEach() मेथड का उपयोग कर सकते हैं। आइए पहले एक दस्तावेज़ के साथ एक संग्रह बनाते हैं।
दस्तावेज़ के साथ संग्रह बनाने की क्वेरी इस प्रकार है:
> 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") }
find() विधि की मदद से संग्रह से सभी दस्तावेज़ प्रदर्शित करें। क्वेरी इस प्रकार है:
> db.studentInformation.find().pretty();
आउटपुट निम्न है:
{ "_id" : ObjectId("5c8bc15780f10143d8431e21"), "StudentName" : "Chris" } { "_id" : ObjectId("5c8bc15e80f10143d8431e22"), "StudentName" : "Robert" } { "_id" : ObjectId("5c8bc17380f10143d8431e23"), "StudentName" : "James" }
MongoDB में क्लोन बनाने की क्वेरी यहाँ है:
> db.studentInformation.find().forEach( function(copyValue){ db.makingStudentInformationClone.insert(copyValue) } );
आइए MongoDB में क्लोन संग्रह के दस्तावेज़ जांचते हैं। क्वेरी इस प्रकार है:
> db.makingStudentInformationClone.find();
आउटपुट निम्न है:
{ "_id" : ObjectId("5c8bc15780f10143d8431e21"), "StudentName" : "Chris" } { "_id" : ObjectId("5c8bc15e80f10143d8431e22"), "StudentName" : "Robert" } { "_id" : ObjectId("5c8bc17380f10143d8431e23"), "StudentName" : "James" }
आइए अब क्लोन सहित सभी संग्रहों की सूची जांचते हैं। क्वेरी इस प्रकार है:
> show collections;
आउटपुट निम्न है:
copyThisCollectionToSampleDatabaseDemo deleteDocuments deleteDocumentsDemo deleteSomeInformation employee getElementWithMaxIdDemo internalArraySizeDemo makingStudentInformationClone prettyDemo selectWhereInDemo sourceCollection studentInformation updateInformation userInformation