Home Ask Login Register

Developers Planet

Your answer is one click away!

yoni February 2016

Mongo compare and import missing data

I have a mongo server receiving data from servers behind an amazon LB, 2 days ago there was an error and part of the servers sent their data to an old mongo server that had a db of the same name, we realized that and fixed it back right away.

Now i have part of my data stored on the wrong machine. What I need now is a way to compare the data between the 2 dbs (which each have 2 relevant collections) and insert only the missing data to the correct collection.

I do not care about the unique id mongo gives but i do need to compare by the field "uuid" that we create.

mongo version: 2.4.4

I am new to Mongo and any help would be greatly appreciated.


Sahil Gulati February 2016

Yes, you can. Follow these steps...

1 mongoexport and then mongoimport on the basis of fields on which you want to compare and import.

  1. mongoexport --db db_name --collection collection_name --type csv --fields field1,field2,field3 --out /var/www/export.csv

once you get the exported CSV at the specified location. Open and remove unwanted fields...

  1. mongoimport --db db_name --collection collection_name --type csv --file /var/www/export.csv --fields field1,field2,field3 --upsertFields field1,field2,field3

1. If you working in production environment, dealing with huge database then free up your mongo from load of queries and then export else it might get stucked.

Post Status

Asked in February 2016
Viewed 3,426 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life