码迷,mamicode.com
首页 > 数据库 > 详细

Python对比两个MongoDB数据库里的Collection数据

时间:2014-09-04 23:39:00      阅读:576      评论:0      收藏:0      [点我收藏+]

标签: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 == other
12 
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...‘ % _id
20             continue
21         if not compare_dict(_first, _second):
22             print ‘document with ObjectId("%s") not same in both databases...‘ % _id
23 
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.message
31     finally:
32         client1.close()
33         client2.close()

 

Python对比两个MongoDB数据库里的Collection数据

标签:style   blog   color   os   io   ar   for   数据   div   

原文地址:http://www.cnblogs.com/zhangjunjun203/p/3957040.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!