3

I have the following three collections in MongoDB

fd2k10sample
fd2k16sample
fd2ksample

the documents in each of them are of same structure so no structure problem as below:

fd2ksample

id  cat1    cat2     cat3   cat4
1   doza    moza     goza   hoza
2   beta    geta     jeta   leta
3   huggy   muggy    guggy  luggy

fd2k10sample

id cat1     cat2    cat3    cat4
1  prizzy   mizzy   tizzy   hizzy
2  chuta    buta    guta    tuta
3  befer    lefer   gefer   tefer

fd2k16sample

id  cat1   cat2   cat3   cat4
1   poopa  doopa  hoopa  loopa
2   nijjy  pijjy  hijjy  tijjy
3   ufha   puhfa  duhfa  tuhfa

How do I combine them like so:

fd2kfullsample

id   cat1  cat2  cat3  cat4
1   doza    moza     goza   hoza
2   beta    geta     jeta   leta
3   huggy   muggy    guggy  luggy
1   poopa  doopa     hoopa  loopa
2   nijjy  pijjy     hijjy  tijjy
3   ufha   puhfa     duhfa  tuhfa
1   poopa  doopa     hoopa  loopa
2   nijjy  pijjy     hijjy  tijjy
3   ufha   puhfa     duhfa  tuhfa

How to do this with just few commands in MongoDB version 3.4?

The answer given in this solution is about Merging the Data Documents whereas I want to combine the data documents not merge them. The below is not a duplicate but a different question altogether: MongoDB: Combine data from multiple collections into one..how?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
gather bar
  • 456
  • 1
  • 9
  • 29
  • Do not agree with the answers @chridam The Answers given there do not address combining they are about merging collections I want to combining as given in the example above. – gather bar Feb 07 '17 at 11:43

1 Answers1

6

You can easily achive this with copyTo()

to merge your 3 collections into one, just run the following commands:

db.fd2k10sample.copyTo("fd2kfullsample")
db.fd2k16sample.copyTo("fd2kfullsample")
db.fd2ksample.copyTo("fd2kfullsample")
felix
  • 9,007
  • 7
  • 41
  • 62
  • Thanks for answering this bang on target. I am using it now and it works. Thanks you saved me a lot of time @felix – gather bar Feb 07 '17 at 11:44
  • Your Question is unique and I faced a similar issue this solution by @felix worked for me Thx for your answer –  Jun 11 '17 at 14:27