Compare a mongo diff on two collections Compare a mongo diff on two collections mongodb mongodb

Compare a mongo diff on two collections


Try the following in the shell, it will iterate each item within a collection and try to match each document based on ID.

Say we have 2 collections db.col1 and db.col2:

> db.col1.find(){ "_id" : 1, "item" : 1 }{ "_id" : 2, "item" : 2 }{ "_id" : 3, "item" : 3 }{ "_id" : 4, "item" : 4 }> db.col2.find(){ "_id" : 1, "item" : 1 }{ "_id" : 2, "item" : 2 }{ "_id" : 3, "item" : 3 }{ "_id" : 4, "item" : 4 }

We can then create a javascript function to compare 2 collections

function compareCollection(col1, col2){    if(col1.count() !== col2.count()){        return false;    }    var same = true;    var compared = col1.find().forEach(function(doc1){        var doc2 = col2.findOne({_id: doc1._id});        same = same && JSON.stringify(doc1)==JSON.stringify(doc2);    });    return same;}

Then call is like the following:

> compareCollection(db.col1, db.col2)true

If we then have a 3rd collections db.col3

> db.col3.find(){ "_id" : 1, "item" : 1 }

And compare this one

> compareCollection(db.col1, db.col3)false

we'll get the expected result.

If we also have a 4th collection which has matching documents but diffrent data db.col4

> db.col4.find(){ "_id" : 1, "item" : 10 }{ "_id" : 2, "item" : 2 }{ "_id" : 3, "item" : 3 }{ "_id" : 4, "item" : 4 }

This will also return false

> compareCollection(db.col1, db.col4)false


Using the Kevin Smith response, I have a new version, only for compare and return those ID's who the collectionB don't have comparing with the collectionA. And save the result in a collectionC when you have lots records.

    db.collectionA.find().forEach(function(doc1){        var doc2 = db.collectionB.findOne({_id: doc1._id});        if (!(doc2)) {                db.collectionC.insert(doc1);        }    });


The dbHash has done the trick:

use db_namedb.runCommand('dbHash')

It returns the hash values for each collection. You can then compare them. It's pretty accurate.