The bulletin board is scraped and stored in MongoDB with Python pymongo.
We would like to add data if it is missing or update it if it exists and the values are different.
How can I do this?
Previous Code
b = dict(title=thread_title, url=thread_url, res=sorted(all_res))
collection.insert(b)
Trying Code
collection.update_one({"$set":{"title": thread_title}}, {"$set":{"url": thread_url}}, {"$set":{"res": sorted(all_res)}}, upsert=True)
Error Code
TypeError Collection.update_one() got multiple values for argument 'upsert'
There are about 200 MongoDB documents available.
_id 62459b8d6ebc7c7b3e14b3ad
field:title "THREAD TITLE"
field:url "URLURLURL"
field:res Array 0 "#100 2022/02/20 13:22 comment blah blah blah" 1 "#101 2022/02/20 13:23 comment blah blah blah"
mongo update official document
In your update query the filter part is missing:
collection.update_one(
{
// Your selection criteria for the update. The same query selectors as in the find() method
"_id" : ObjectId("62459b8d6ebc7c7b3e14b3ad") //for example
},
{
"$set":{
"title": thread_title,
"url": thread_url,
"res": sorted(all_res)
},
upsert=True
})
The above query will update the document with id = 62459b8d6ebc7c7b3e14b3ad in your collection