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