5

I want to implement HTTP PATCH using Python Flask framework. As an input, I would be receiving JSON patch like:

[
  { "op": "replace", "path": "/work/title", "value": "Senior Engineer" }
]

My database is MongoDB where I want to apply the above patch directly to Mongo. For example, below is the document stored in Mongo:

{  
   "name":"ABC",
   "age":25,
   "work":{  
      "title":"Engineer",
      "company":"XYZ"
   }
}

After applying the patch, it should be:

{  
   "name":"ABC",
   "age":25,
   "work":{  
      "title":"Senior Engineer",
      "company":"XYZ"
   }
}

Could you please help me to find a way to implement?

According to my research, I found a Python module python-json-patch which helps to apply json to patch to a json object. So, we would need to get json/document from the MongoDB and apply the patch using above module. Then replace the document back in MongoDB. So, basically this would end up in PUT rather than PATCH.

One more approach I thought to have a module to parse the json patch and construct the json and apply the update to MongoDB using $set. But this approach is naive and not efficient.

So, please suggest a good way to implement HTTP PATCH using json patch and directly applying to the MongoDB document.

Isan Sahoo
  • 384
  • 2
  • 10
  • An example in TerminusDB that creates a patch and then generates a MongoDB query to execute it is here: https://github.com/terminusdb/terminusdb-tutorials/blob/master/diff_patch/diff_patch.js – Gavin Mendel-Gleason Feb 16 '22 at 15:27

0 Answers0