Python 更新mongodb中的唯一记录

Python 更新mongodb中的唯一记录,python,mongodb,pymongo,Python,Mongodb,Pymongo,在mongo数据库中,日期是唯一的键。要插入的记录如下所示,其中结果包含多个字典 {'date': 1435363200, 'result': [{'article_link': u'http://gadgets.ndtv.com/mobiles/reviews/lenovo-k3-note-first-impressions-the-complete-package-708138?trendingnow', 'img': u'http://cdn.ndtv.com/tech/images/ga

在mongo数据库中,日期是唯一的键。要插入的记录如下所示,其中结果包含多个字典

{'date': 1435363200, 'result': [{'article_link': u'http://gadgets.ndtv.com/mobiles/reviews/lenovo-k3-note-first-impressions-the-complete-package-708138?trendingnow', 'img': u'http://cdn.ndtv.com/tech/images/gadgets/thumb/lenovo_k3_note_back_ndtv_small.jpg', 'title': u'Lenovo K3 Note First Impressions: The Complete Package?'}, {'article_link': u'http://www.ndtv.com/india-news/rajasthan-chief-minister-vasundhara-raje-to-attend-niti-aayog-meeting-in-delhi-today-775766?trendingnow', 'img': u'http://i.ndtvimg.com/i/2015-06/vasundhara-raje-240-pti-neeti-aayog_240x180_41435392178.jpg', 'title': u'Vasundhara Raje Attends NITI Aayog Meet in Delhi, Returns to Jaipur Without Meeting BJP Leaders'}]}
在同一日期,可能有多个插入具有不同的结果值,有时与前一个插入相同

#result = {'date': 1435363200, 'result': [{'article_link': u'http://gadgets.ndtv.com/mobiles/reviews/lenovo-k3-note-first-impressions-the-complete-package-708138?trendingnow', 'img': u'http://cdn.ndtv.com/tech/images/gadgets/thumb/lenovo_k3_note_back_ndtv_small.jpg', 'title': u'Lenovo K3 Note First Impressions: The Complete Package?'}, {'article_link': u'http://www.ndtv.com/india-news/rajasthan-chief-minister-vasundhara-raje-to-attend-niti-aayog-meeting-in-delhi-today-775766?trendingnow', 'img': u'http://i.ndtvimg.com/i/2015-06/vasundhara-raje-240-pti-neeti-aayog_240x180_41435392178.jpg', 'title': u'Vasundhara Raje Attends NITI Aayog Meet in Delhi, Returns to Jaipur Without Meeting BJP Leaders'}, {'article_link': u'http://www.ndtv.com/india-news/high-commissioner-to-new-zealand-posted-back-to-delhi-after-wife-accused-of-assault-775813?trendingnow', 'img': u'http://i.ndtvimg.com/i/2015-06/ravi-thapar_240x180_81435381614.jpg', 'title': u"High Commissioner to New Zealand 'Posted Back' to Delhi After Wife Accused of Assault"}]}
#result = {'date': 1435363200, 'result': [{'article_link': u'http://gadgets.ndtv.com/mobiles/reviews/lenovo-k3-note-first-impressions-the-complete-package-708138?trendingnow', 'img': u'http://cdn.ndtv.com/tech/images/gadgets/thumb/lenovo_k3_note_back_ndtv_small.jpg', 'title': u'Lenovo K3 Note First Impressions: The Complete Package?'}, {'article_link': u'http://www.ndtv.com/india-news/rajasthan-chief-minister-vasundhara-raje-to-attend-niti-aayog-meeting-in-delhi-today-775766?trendingnow', 'img': u'http://i.ndtvimg.com/i/2015-06/vasundhara-raje-240-pti-neeti-aayog_240x180_41435392178.jpg', 'title': u'Vasundhara Raje Attends NITI Aayog Meet in Delhi, Returns to Jaipur Without Meeting BJP Leaders'}, {'article_link': u'http://profit.ndtv.com/news/economy/article-world-economy-may-be-slipping-into-1930s-depression-raghuram-rajan-775521?pfrom=home-lateststories&trendingnow', 'img': u'http://i.ndtvimg.com/i/2015-06/raghuram-rajan_240x180_61435303075.jpg', 'title': u'Raghuram Rajan Says, World Economy May Be Slipping Into 1930s Depression'}, {'article_link': u'http://www.ndtv.com/diaspora/bobby-jindal-wants-to-get-rid-of-us-supreme-court-775793?trendingnow', 'img': u'http://i.ndtvimg.com/i/2015-06/bobby-jindal-announcement-afp_240x180_41435200334.jpg', 'title': u'Bobby Jindal Wants to Get Rid of US Supreme Court'}, {'article_link': u'http://auto.ndtv.com/news/will-india-go-crazy-over-the-hyundai-creta-775784?trendingnow', 'img': u'http://i.ndtvimg.com/i/2015-06/hyundai-creta_240x180_51433745765.jpg', 'title': u'Will India Go Crazy Over the Hyundai Creta?'}]}
问题是,在每次插入期间,数据库中只应追加新的不同词典

这是我的代码,看起来很有效,任何改进或更好的方式都值得赞赏:

    try:
        self.collection.insert(record)
        print "mongo done"
    except Exception:
        print 'Failed to save value '
        date = self.getTodayDate()
        data = self.collection.find({'date': record['date']})
        new_key = []
        for val in record['result']:
            #convert input values in unicode, as data fetched from mongo comes in this format
            new_key.append({ unicode(key):unicode(value) for key,value in val.items() })
        for key in data:
            #Append only new unique values in new_key
            new_key.extend([k for k in key['result'] if k not in new_key])
        self.collection.update(
                {'date' : record['date']},
               { '$set' : {'result': new_key}},
               True
        )