Python compares collection data in two MONGODB databases

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.