MongoDB - Pull from array of objects -


i have collection

{     "_id" : objectid("534bae30bf5049a522e502fe"),     "data" : [             {                     "0" : {                             "content" : "1",                             "type" : "text",                             "ident" : true                     },                     "1" : {                             "content" : "",                             "type" : "text",                             "ident" : false                     }              },             {                     "0" : {                             "content" : "2",                             "type" : "text",                             "ident" : true                     },                     "1" : {                             "content" : "",                             "type" : "text"                     }             }     ] 

}

content unique.

how remove object matches content: '2'?

i have tried this:

data:{$pull:{"content": deletions[i]}} 

where deletions [i] content.

and several variations, can not work. missing?

as per comment, should worried. have seen few times particularly php applications ( , php has funny kind of notation in dumping arrays ).

the problem elements such "0" , "1" create sub-documents in themselves, , opposed leaving under structure sub-document in the array member run problem accessing individual members of array paths used need "absolute".

so "forces" no other possible option access elements equivalent "dot notation" form. except in case it's not "nth" element of array, actual path need address.

but if work you, , seem "someone" trying avoid problems positional updates under "nested" arrays ( see positional $ operator documentation details ) update can performed this:

the basic statement follows:

db.collection.update(     {         "data.0.context": 2     },     {         "$pull": { "data.$.0.context": 2 }     } ) 

that "seem" bit of funny way write this, on investigating actual structure have should able see reasons why needed. meets requirements of using positional $ operator indicate index of first matched element in array ( aka "data" ) , uses standard sub-document notation in order specify path element updated.

so of course poses problem if element in unknown position. thing consider usage of array important given documented limitation? if yo need match position of "inner" element, change structure put arrays in there.

but understand effects of limitation, , try model according engine can do.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -