mike
mike

Reputation: 397

how to copy document and create new document with new filed value in mongodb

I want to find one document and clone/copy that document and create 100 new documents with new value for few fields using shell script in mongodb.

Below is my document

{
  "_id" : ObjectId("5ef59bde562c9824176e9f20"),
  "productDefinition" : {
    "product" : {
      "companies" : {
        "company" : {
          "productionformation" : {
            "productNumber" : "E128",
            "venderNumber" : "0470",
            "venderName" : "ALPHA SERVICES LLC"           
          }
        }
      }
    }
  },
  "executionId" : "123456"
}

After executing the shell script, i want to have new 100 collection with new values for the below fields

"executionId" : "NewExecutionId" // This value will be Fixed for all new 100 documents "productNumber" : "1" //This value will be increasing.. for first document 1, for second document 2, etc.. "venderNumber" : "1" //This value will be increasing.. for first document 1, for second document 2, etc..

My new collection will be looking like this.

First new document

{
  "_id" : ObjectId("5ef59bde562c9824176e9f20"),
  "productDefinition" : {
    "product" : {
      "companies" : {
        "company" : {
          "productionformation" : {
            "productNumber" : "1",
            "venderNumber" : "1",
            "venderName" : "ALPHA SERVICES LLC"           
          }
        }
      }
    }
  },
  "executionId" : "newExecutionId"
}

Second new document

{
  "_id" : ObjectId("5ef59bde562c9824176e9f20"),
  "productDefinition" : {
    "product" : {
      "companies" : {
        "company" : {
          "productionformation" : {
            "productNumber" : "2",
            "venderNumber" : "2",
            "venderName" : "ALPHA SERVICES LLC"           
          }
        }
      }
    }
  },
  "executionId" : "newExecutionId"
}

Third new document

{
  "_id" : ObjectId("5ef59bde562c9824176e9f20"),
  "productDefinition" : {
    "product" : {
      "companies" : {
        "company" : {
          "productionformation" : {
            "productNumber" : "3",
            "venderNumber" : "3",
            "venderName" : "ALPHA SERVICES LLC"           
          }
        }
      }
    }
  },
  "executionId" : "newExecutionId"
}

Like this fourth document , fifth document, etc... till 100th document...

I tried with this script. but its not working.

copy = db.myCollection.find({"executionId" : "123456", 
 "productDefinition.product.companies.company.productionformation.productNumber" : "E128" , 
 "productDefinition.product.companies.company.productionformation.venderNumber" :"0470" })

for (var i = 1; i< 101; i++){ 
    copy.executionId = "newExecutionId";
    copy.productDefinition.product.companies.company.productionformation.productNumber = i;
    copy.productDefinition.product.companies.company.productionformation.venderNumber" = i;
    db.myCollection.insert(copy);
}

Upvotes: 2

Views: 817

Answers (1)

Abhishek Singh
Abhishek Singh

Reputation: 141

You will be needing to fix following things:

  • Use findOne instead of find as it will return single matching document.
  • Use let (instead of var) while running the loop because there are asynchronous DB operations in loop body.
  • Similarly, create a Deep copy of matchedDoc result / (copy variable) inside for loop body, to avoid updating same object's reference value.

Hope it helps !

Upvotes: 1

Related Questions