0

Here is my code:

from flask_pymongo import PyMongo

app.config['MONGO_DBNAME'] = 'TestDB'
app.config["MONGO_URI"] = "mongodb://localhost:27017/TestDB"
mongo = PyMongo(app)

filePathHash = "fhdsfl5324hfd"
score = 25
db_operations = mongo.db.sample_table
db_operations.insert({"filePathHash": filePathHash, "score": score})

As you can see, I am inserting a record and the above code works fine. What should I do to achieve the below functionality?

  • Check if the record with the value of filePathHash already exists in the db. If yes then update that corresponding document, else insert it as a new record.
LEE
  • 3,335
  • 8
  • 40
  • 70

1 Answers1

1

What you want to do is pass in the "upsert" argument:

db_operations.update({"filePathHash": filePathHash}, {"score": score}, upsert=True)
user1321988
  • 513
  • 2
  • 6