2015-12-08 34 views
0

如果數據不重複,我想檢查文件中的字段(pv_time),然後在pv_time和其他字段中插入數據。在其他字段中允許複製。使用$addToSet我試圖做到這一點。

這裏是我的Python代碼:

for row in results.get('rows'): 
    path = row[0] 
    feedbackId = row[1] 
    pvDate = row[2]+' '+row[3]+':'+row[4] 
    city = row[5] 
    country = row[6] 
    pageviews = int(row[7]) 
    db.customer_feedback_requests_archive.update({'feedback_request_id':ObjectId(feedbackId)},{'$addToSet':{'pv_time.'+path:pvDate},'$push':{'pv_city.'+path:city,'pv_country.'+path:country},'$inc':{'pv_count.'+path:pageviews}}) 

如果我跑這第一次是給了

{ 
     "_id" : ObjectId("558d3900996f95a24aa69ef3"), 
     "feedback_request_id" : ObjectId("5665015a882a5174379d4dbd"), 
     "pv_count" : { 
       "main-rating" : 2 
     }, 
     "pv_city" : { 
       "main-rating" : [ 
         "Bengaluru", 
         "Bengaluru" 
       ] 
     }, 
     "pv_country" : { 
       "main-rating" : [ 
         "India", 
         "India" 
       ] 
     }, 
     "pv_time" : { 
       "main-rating" : [ 
         "20151208 10:00", 
         "20151208 10:01" 
       ] 
     } 
} 

但是,如果我運行此作業兩次,然後它給:

{ 
     "_id" : ObjectId("558d3900996f95a24aa69ef3"), 
     "feedback_request_id" : ObjectId("5665015a882a5174379d4dbd"), 
     "pv_count" : { 
       "main-rating" : 4 
     }, 
     "pv_city" : { 
       "main-rating" : [ 
         "Bengaluru", 
         "Bengaluru", 
         "Bengaluru", 
         "Bengaluru" 
       ] 
     }, 
     "pv_country" : { 
       "main-rating" : [ 
         "India", 
         "India", 
         "India", 
         "India" 
       ] 
     }, 
     "pv_time" : { 
       "main-rating" : [ 
         "20151208 10:00", 
         "20151208 10:01" 
       ] 
     } 
} 

我想要pv_citypv_country中的重複值只有在pv_time是不同的,第二次我期待如果pv_time沒有更新,那麼它不應該更新pv_citypv_country

+0

是否有特定的原因,你爲什麼這樣構建你的文件? – tonyl7126

+0

因此,只有在當前pv_time與集合中的所有值不同的情況下,才需要將值添加到pv_city和pv_country中? – blackmamba

+0

@ tonyl7126是的,還有一些原因 – imSonuGupta

回答

1

它相當簡單,你只需要擴展你的查詢一點點。

db.customer_feedback_requests_archive.update(
    {'feedback_request_id':ObjectId(fee‌​dbackId),'pv_time.'+path:{'$ne':pvDate}}, 
    {'$addToSet':{'pv_time.'+path:pvDate},'$‌​push':{'pv_city.'+path:city,'pv_country.'+path:country},'$inc':{'pv_count.'+path:‌​pageviews}} 
) 

額外查詢參數的作用是,它會搜索數組是否已經有日期。如果它不存在,更新將會觸發,這將解決您的問題。