MongoDB Update Array element -
i have document structure like
{ "_id" : objectid("52263922f5ebf05115bf550e"), "fields" : [ { "field" : "lot no", "rules" : [ ] }, { "field" : "rma no", "rules" : [ ] } ] }
i have tried update using following code push rules array hold objects.
db.test.update({ "fields.field":{$in:["lot no"]} }, { $addtoset: { "fields.field.$.rules": { 'item_name': "my_item_two", 'price':1 } } }, false, true);
but following error can't append array using string field name [field]
how do update? have searched thru similiar posts nothing clicking. help.
you gone deep wildcard $
. match item in fields
array, access on that, with: fields.$
. expression returns first match in fields
array, reach fields fields.$.field
or fields.$.result
.
now, lets update update
:
db.test.update({ "fields.field": "lot no" }, { $addtoset: { "fields.$.rules": { 'item_name': "my_item_two", 'price':1 } } }, false, true);
please note i've shortened query
equal expression.
Comments
Post a Comment