Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 :)

share|improve this question

3 Answers 3

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

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

db.prices.find()

  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 :) ...

share|improve this answer
    
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 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.

share|improve this answer

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("12.34.56.78:27017", "mydb.mycollectionname")

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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