MongoDB: How do I update a single subelement in an array, referenced by the index within the array?

As expected, the query is easy once you know how. Here’s the syntax, in python:

db["my_collection"].update(
    { "_id": ObjectId(document_id) },
    { "$set": { 'documents.'+str(doc_index)+'.content' : new_content_B}}
)

Leave a Comment