0
votes

Pymongo, Python3. Really racking my brains. Tried update_one() and find_one()andupdate(). Tried with the mongo id "_id" as well as with a know ID that I set in each document. Nothing updates in the documents. Can anyone please show me where I m going wrong? See below for last attempts. Want to loop over all collections and all documents in each collection and update a specific field.

With update_one() and an ID I know to exist in the document:

for collection in collectionNames:
    for doc in movie_db[str(collection)].find():
        new_path = get_updated_poster()
        new_path_url = base_url + new_path
        result = movie_db.collection.update_one({'id':int(ID)},{'$set':{'poster_path':new_path_url}})

With find_one_and_update(), even tried with copying an "_id" to no avail (note that I here also have "from bson.objectid import ObjectId" added:

for collection in collectionNames:
    for doc in movie_db[str(collection)].find():
        new_path = get_updated_poster()
        new_path_url = base_url + new_path
        result = movie_db.collection.update_one(
                     {"_id":ObjectId(id)}, 
                     {"$set": {
                         "poster_path": new_path
                     }
                     }, upsert=False)
1
Remove the type casts in conditions, then if you still have difficulties post the documents and conditions you are using. - D. SM

1 Answers

1
votes

movie_db.collection.update_one usually means to update one document in the collection named "collection".

You appear to be using collection to hold the name of your collection, so you probably need to use movie_db[str(collection)].update_one