Home > Blockchain >  How do I update/add multiple DBs in pymongo?
How do I update/add multiple DBs in pymongo?

Time:04-01

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"

CodePudding user response:

If you try to 'separate' your line code, you will see that you're giving the function 4 arguments, each set you gave is an argument so upsert is taking as value {"$set":{"res": sorted(all_res)}}, you shouldn't separate the 'update' argument

In addition to that, the update_one function take a filter (it's not an optional argument, you should always give a filter).

There is the definition of the update_one function

update_one(filter, update, upsert=False, bypass_document_validation=False, collation=None, array_filters=None, hint=None, session=None)

And finally, if you want to update multiple instances at the same time you should consider update_many function (it basically works the same way as update_one).

This code should work, and remember to add a filter.

collection.update_one(
    {
        # your filter here for exemple
        "id" : id
    },
    {
        "$set":{
            "title": thread_title,
            "url": thread_url,
            "res": sorted(all_res)
        }
    }, 
    upsert=True)

CodePudding user response:

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

  • Related