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_city
和pv_country
中的重複值只有在pv_time
是不同的,第二次我期待如果pv_time
沒有更新,那麼它不應該更新pv_city
和pv_country
。
是否有特定的原因,你爲什麼這樣構建你的文件? – tonyl7126
因此,只有在當前pv_time與集合中的所有值不同的情況下,才需要將值添加到pv_city和pv_country中? – blackmamba
@ tonyl7126是的,還有一些原因 – imSonuGupta