I have a collection in a mongoDB, on a project that runs locally, and have to copy the collection to the dev server. Can you tell me how to do this, considering that:

  1. The local machine (the source) is accessible from the outside, but only with the IP.

  2. The version of the local mongo is 2.0.4, while the version of mongo on the dev server is 2.2.2.

I made a little research, but I'm not sure for some things ... like for example which credentials should I choose for the source DB ? ... the ones for SSH connection? ... or ... ? ...

Thanks in advance :)


You can create another connection from the destination server and use the save() function.

For example:

conn2 = new Mongo( "desitination_server:port")

desti_db = conn2.getDB( "destination_db_name" ) --> now you can access destination mongodb server from source DB connection.

db.sourceCollection.find().forEach( function(x){ desti_db.targetCollectionName.save(x) } ); --> read current source DB collection and save it in destination mongodb

  • We're on v2.2.0 for the source db and v2.4.9 for the target db, and cloneCollection wasn't working. This did exactly what I needed! – Alan Jul 8 '16 at 16:05

The best solution I managed to think of is this one:

  1. I made a dump of the source collection data:


  1. Modify the result (list of JSONs) from something like

{"_id" : ObjectId("..."), "action" : "show", "price" : 12 } {"_id" : ObjectId("..."), "action" : "hide", "price" : 10 } {"_id" : ObjectId("..."), "action" : "poke", "price" : 20 } ................

to a such one (added "," after each JSON, removed the ID, so I have an array at the end):

[ {"action" : "show", "price" : 12 }, {"action" : "hide", "price" : 10 }, {"action" : "poke", "price" : 20 } ................ ]

And then pass this new array of JSONs to the insert method of the new collection: (copied the array in the clipboard, directly from the text editor I used to make it)

db.prices.inset( + Shift+Insert + )

And voila :) ... all your data is transferred :) ...

  • Thanks, that worked for me. My requirement was to copy some data (user logins) from one mongodb server to another on another network where the only connection between them is that I can ssh to each of the servers. Some shell script magic with the above information resulted in this: echo 'db.users.find({"email":"user@domain.com"});' | ssh server1 mongo --quiet localhost/dbname | ruby -rjson -e 'o=[];$<.read.split("\n").each { |l| o << JSON.parse(l.gsub(/"_id.*?,/,"")); }; puts JSON[o]' | while read data; do echo "db.users.insert($data)" | ssh server2 mongo localhost/dbname; done – Guss Aug 28 '14 at 11:47

First, I would advise you to upgrade your MongoDB versions - both your dev and local versions are very old, and you should have them both at the same version if at all possible. The 2.0 and 2.2 versions are very different (no global lock in 2.2) and both have had multiple stable releases since the versions you list. Finally, both versions are considered end of life.

As for how to transfer data between them, you have several options, but if the data is relatively small, then the easiest options is mongodump. Simply use that to dump out the local data you wish to transfer to BSON (will include indexes etc. if you take a whole collection or database) and then use mongorestore to insert that data into the dev database. Again, I would recommend using a newer version of mongodump/mongorestore than 2.2.2 or 2.0.4 - there have been several bugs fixed with the tools in later versions that you may hit otherwise (the later versions of the tools should be backwards compatible).

In terms of other options, if you are taking an entire database, then you can simply copy the entire set of files for that database from the local host to the dev server. You would need to restart the database, and it would mean wiping that database out each time on the dev server, but depending on your needs that might be an option. You can also look at the mongo connector as a way to mirror your data between local and dev in a more real time and ongoing fashion. There are likely a couple of other ways to achieve this too, but these are the most straight forward.


Why don't you try db.cloneCollection() ? http://docs.mongodb.org/manual/reference/method/db.cloneCollection/

you would run db.cloneCollection(from_instance, collection_name, query) on the dev mongodb instance. from_instance would be the IP and port address of the local instance. And query is optional - if not entered it will pull all of the documents in the source collection.

so, something like: db.cloneCollection("", "mydb.mycollectionname")


MongoCopyPaste: look for any registry at remote server. Copy and Paste in destiny server. From one to many collections.


  • How does your project deal with the different versions of mongodb? – chicks May 1 '17 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.