Python對比兩個MongoDB資料庫裡的Collection資料

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   ar   for   資料   div   

 1 #coding=utf-8 2 from pymongo import MongoClient 3  4 def get_all_ids(collection): 5     for each in collection.find(): 6         yield each[‘_id‘] 7          8 def compare_dict(this, other): 9     del this[‘_id‘]10     del other[‘_id‘]11     return this == other12 13 def compare(_first_collection, _second_collection):14     print ‘Total documents are (first:%d, second:%d)‘ % (_first_collection.count(), _second_collection.count())15     for _id in get_all_ids(_first_collection):16         _first = _first_collection.find_one({‘_id‘:_id})17         _second = _second_collection.find_one({‘_id‘:_id})18         if not (_first and _second):19             print ‘document with ObjectId("%s") not in both databases...‘ % _id20             continue21         if not compare_dict(_first, _second):22             print ‘document with ObjectId("%s") not same in both databases...‘ % _id23 24 if __name__ == ‘__main__‘:  25     client1 = MongoClient(‘localhost‘, 27017)26     client2 = MongoClient(‘localhost‘, 19871)27     try:28         compare(client1.NewRisDatabase.NewRisCollection, client2.NewRisDatabase.NewRisCollection)29     except Exception as e:30         print e.message31     finally:32         client1.close()33         client2.close()

 

Python對比兩個MongoDB資料庫裡的Collection資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.