如何在 mongodb 中检查 $addToSet return true 或 false?
How to check $addToSet return true or false in mongodb?
我想检查文档中的字段 (pv_time
),如果数据不重复,则在 pv_time
和其他字段中插入数据 also.In 允许其他字段重复。使用 $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
.
它相当简单,您只需稍微扩展一下查询即可。
db.customer_feedback_requests_archive.update(
{'feedback_request_id':ObjectId(feedbackId),'pv_time.'+path:{'$ne':pvDate}},
{'$addToSet':{'pv_time.'+path:pvDate},'$push':{'pv_city.'+path:city,'pv_country.'+path:country},'$inc':{'pv_count.'+path:pageviews}}
)
额外查询参数的作用是,如果数组中已经有日期,它会搜索。如果它不存在,将启动更新,这将解决您的问题。
我想检查文档中的字段 (pv_time
),如果数据不重复,则在 pv_time
和其他字段中插入数据 also.In 允许其他字段重复。使用 $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
.
它相当简单,您只需稍微扩展一下查询即可。
db.customer_feedback_requests_archive.update(
{'feedback_request_id':ObjectId(feedbackId),'pv_time.'+path:{'$ne':pvDate}},
{'$addToSet':{'pv_time.'+path:pvDate},'$push':{'pv_city.'+path:city,'pv_country.'+path:country},'$inc':{'pv_count.'+path:pageviews}}
)
额外查询参数的作用是,如果数组中已经有日期,它会搜索。如果它不存在,将启动更新,这将解决您的问题。