1

Is there a way to clone a collection or entire Mongodb database (my databases only have one collection so both options are OK) with Mongoose? I saw that there is a possibility to execute raw Mongo commands with Mongoose. What command can I use to clone an entire collection or db from one db to another?

Thanks in advance.

  • If this is not possible, is there a way to do it with native MongoDB for node instead of Mongoose? –  Jun 23 '19 at 18:13
  • [db.cloneCollection()](https://docs.mongodb.com/manual/reference/method/db.cloneCollection/)? – Matin Sasan Jun 23 '19 at 20:58
  • Is that possible with Mongoose? I looked at that option but it did not seem to work. @MatinSasan –  Jun 24 '19 at 10:24
  • In Mongoose Docs I only found out [Schema.prototype.clone()](https://mongoosejs.com/docs/api.html#schema_Schema-clone), which is, I think, not what you want. As for your Mongoose question, here's the answer that I found that (`There is no direct possibility in mongoose.`): https://stackoverflow.com/a/25470961/11330560. I upvoted you and hope someone someday answer this better. That is my question too. – Matin Sasan Jun 24 '19 at 10:48
  • 1
    @MatinSasan Thank you for your comments, indeed, I think it is not possible. The other options are to use Mongodb native driver for Node.js or to make a script which clones/copies the database in shell and automate this script. –  Jun 24 '19 at 10:56

1 Answers1

0

I had a hard time doing this I don't have any reference.

However, this is how I did on my end.

1, I created another collection within the same

db: mydb
collections: books, oldbooks

2, Since I only know how to connect to one database at a time, I stick to this:

mongoose.connect(process.env.CONN_STR);

3, On your existing collection, in this case, books, we have this code:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

var BookSchema = new Schema({
  name: String
})

module.exports = mongoose.model('Book', BookSchema);

4, I created a different Schema for the backup so I can specific the name of the collection:

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    var ObjectId = Schema.ObjectId;

    var BackupSchema = new Schema({
      name: String
    }, {
      collection: 'oldbooks'
    })

    module.exports = mongoose.model('BackupBook', BackupBookSchema);

NOTICE: that we specified the collection in BackupBook Schema collection: 'oldbooks'. The idea is to replicate the existing schema to the backup schema.

5, Fetch and save each entry in the collection:

 Book.find()
    .exec((err, books) => {
      if(err) throw err
      else {
        books.forEach( (book) => {
          var backup = new BackupBook();

          backup._id = book._id;
          backup.name = book.name;

          backup.save((err, backup) => {
          })
        })
      }
    })

TLDR: Create a different collection as backup. Query each entry of the collection then save to the backup Schema individually. Note, the backup schema must specify the name of the collection.

Vince Banzon
  • 1,324
  • 13
  • 17