1 #Coding=utf-82 fromPymongoImportmongoclient3 4 defGet_all_ids (collection):5 foreachinchcollection.find ():6 yieldeach['_id']7 8 defCompare_dict (This, other):9 delthis['_id']Ten delother['_id'] One returnthis = = Other A - defCompare (_first_collection, _second_collection): - Print 'Total Documents is (first:%d, second:%d)'%(_first_collection.count (), _second_collection.count ()) the for_idinchGet_all_ids (_first_collection): -_first = _first_collection.find_one ({'_id': _id}) -_second = _second_collection.find_one ({'_id': _id}) - if not(_first and_second): + Print 'document with ObjectId ('%s ') not in both databases ...'%_id - Continue + if notcompare_dict (_first, _second): A Print 'document with ObjectId ('%s ') not same in both databases ...'%_id at - if __name__=='__main__': -CLIENT1 = Mongoclient ('localhost', 27017) -Client2 = Mongoclient ('localhost', 19871) - Try: - Compare (client1. Newrisdatabase.newriscollection, Client2. Newrisdatabase.newriscollection) in exceptException as E: - PrintE.message to finally: + client1.close () -Client2.close ()
Python compares collection data in two MONGODB databases