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


i'm trying update single subelement contained within array in mongodb document. want reference field using array index. (elements within array don't have fields can guarantee unique identifiers.) seems should easy do, can't figure out syntax.

here's want in pseudo-json.

before:

{ _id : ..., other_stuff ... , my_array : [ { ... old content ... }, { ... old content b ... }, { ... old content c ... } ] } 

after:

{ _id : ..., other_stuff ... , my_array : [ { ... old content ... }, { ... new content b ... }, { ... old content c ... } ] } 

seems query should this: (again, pseudocode)

db.my_collection.update( {_id: objectid(document_id), my_array.1 : 1 }, {my_array.$.content: new content b } ) 

but doesn't work. i've spent way long searching mongodb docs, , trying different variations on syntax (e.g. using $slice, etc.) can't find clear explanation of how accomplish kind of update in mongodb. please help!

as expected, query easy once know how. here's syntax, in python:

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

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -