Back

Copying a DataBase / Collection

Here we look at coping / cloning a MongoDB Collection into another DataBase.

// The Collection to be copied
db.Net4orce.Canvas.General.WebPages

  // find, this returns all but, if
  // required, you can filter which docs to copy
  .find({})

  // Loop over matching documents
  .forEach( function(o) { 

      // getSiblingDB - to copy to another DB
      // on the same mongod instance
      db.getSiblingDB(
        'the-other-database-v0-2-0'
      )['Net4orce.Canvas.General.WebPages']
        .insert(o);

    }         
  );