-1

I am trying to update a mongodb collection using python. I am trying to update the collection using the ObjectId but it is not working.

result = myCollection.update_one(
    {_id:"53298scc1c29d0s036c"},
    {
        "$set": {
            "lname":"Jones"
        }
    }
)
HABosch
  • 49
  • 3
  • 1
    Does this answer your question? [How to update values using pymongo?](https://stackoverflow.com/questions/13710770/how-to-update-values-using-pymongo) – whoami - fakeFaceTrueSoul Jan 23 '20 at 20:49
  • Hi! It would be easier to answer your question with a bit more detail. e.g. are there any errors? What output are you getting vs what are you expecting? "It is not working" doesn't really help people help you. I would recommend reading [this link](https://stackoverflow.com/help/how-to-ask) for more info. Happy coding! – thinkOfaNumber Jan 24 '20 at 01:15

2 Answers2

0

You just need to cast your _id value to be ObjectId type.

from bson import ObjectId

result = myCollection.update_one(
{_id: ObjectId("53298scc1c29d0s036c")},
{
    "$set": {
        "lname":"Jones"
    }
})
Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
0
from bson import ObjectId

result = myCollection.update_one(
{"_id": ObjectId("53298scc1c29d0s036c")},
{
    "$set": {
        "lname":"Jones"
    }
})
Ananthu M
  • 77
  • 5