2017-12-27 209 views
1

我想为多个文档做一个upsert。目前,我尝试'多=真'。它不适合我。TypeError:update()得到关键字参数'upsert'的多个值

我的代码是

dataArray = [] 
for i in posts_data['posts']['data']: 

    dataPost = {} 
    dataPost['post_id'] = i['id'] 
    dataPost['post_message'] = json.dumps(i['message']) 

    comments_in_posts = graph.get_object(dataPost['post_id'], fields = 'comments') 

    dataPost['comments_data'] = json.loads(json.dumps(comments_in_posts)) 
    reactions_in_post = graph.get_object(dataPost['post_id'], fields = 'reactions') 


    dataPost['reactions_data'] = json.loads(json.dumps(reactions_in_post)) 
    getPostReactionCount(dataPost['reactions_data']) 

    dataArray.append(dataPost) 



#insert process 
print "before insert" 
print "-----------------------------------------------------------" 
collection.update({'post_id':dataPost['post_id']}, {'post_message':dataPost['post_message']}, {'comments_data':dataPost['comments_data']},{'reactions_data':dataPost['reactions_data']}, multi=True, upsert=True) 
+0

'collection'的类型是什么? – Zack

+0

嗨扎克!实际上前面有一些代码,但我并没有在这里粘贴所有的代码。该集合实际上是我的mongodb中的集合。所以这里是我的代码。 dataArray中的= [] 客户端= MongoClient( '本地主机:27017') DB = client.sDB 集合= db.sInsert – xxxSL

+1

有几件事情在这里澄清一下。 'collection.update'嵌套在循环中吗?我不清楚你想要填写什么文件。你能澄清吗? –

回答

0

似乎你正在处理错误的变量 你填充所有结果到

dataArray.append(*dataPost*) 

但下次你使用的更新与去年dataPost

collection.update({'post_id':dataPost['post_id']}, {'post_message':dataPost['post_message']}, {'comments_data':dataPost['comments_data']},{'reactions_data':dataPost['reactions_data']}, multi=True, upsert=True) 

所以我的意思只是把更新陈述循环导致你不能只是给该阵列的更新命令 - 它不会理解的结构,也只是FYI有updateMany命令集可用只是更明显地阅读

相关问题