document - I need to delete a subdocument in MongoDB -


this structure:

{     "_id" : objectid("52ebf3ba71616b871600000c"),     "components" : [         {             "type" : "text",             "text_type" : "subtitle",             "pos_x" : 198.384521484375,             "pos_y" : 114.43489074707031,             "content" : "new subtitle",             "font" : "",             "font_size" : "",             "color" : "",             "bold" : false,             "italic" : false,             "underlined" : false,             "rotation" : 0,             "scale" : 0,             "custom_css" : "",             "_id" : objectid("52ebf3c171616b871600000d")         },         {             "type" : "text",             "text_type" : "title",             "pos_x" : 198.384521484375,             "pos_y" : 114.43489074707031,             "content" : "new title",             "font" : "",             "font_size" : "",             "color" : "",             "bold" : false,             "italic" : false,             "underlined" : false,             "rotation" : 0,             "scale" : 0,             "custom_css" : "",             "_id" : objectid("52ebf3c371616b871600000e")         },         {             "type" : "text",             "text_type" : "title",             "pos_x" : 279.32373046875,             "pos_y" : 265.3794403076172,             "content" : "new title",             "font" : "",             "font_size" : "",             "color" : "",             "bold" : false,             "italic" : false,             "underlined" : false,             "rotation" : 0,             "scale" : 0,             "custom_css" : "",             "_id" : objectid("52ebf44471616b871600000f")         },         {             "type" : "text",             "text_type" : "subtitle",             "pos_x" : 55.32373046875,             "pos_y" : 35.37944030761719,             "content" : "new subtitle",             "font" : "",             "font_size" : "",             "color" : "",             "bold" : false,             "italic" : false,             "underlined" : false,             "rotation" : 0,             "scale" : 0,             "custom_css" : "",             "_id" : objectid("52ebf44571616b8716000010")         },         {             "type" : "image",             "file" : "",             "external_url" : "",             "size" : 40,             "pos_x" : 0,             "pos_y" : 0,             "rotation" : 0,             "scale" : 0,             "custom_css" : "",             "_id" : objectid("52ebf4d971616b8716000011")         }     ],     "number" : 0,     "pos_x" : 0,     "pos_y" : 0,     "presentation_id" : 46,     "rotation_x" : 0,     "rotation_y" : 0,     "rotation_z" : 0,     "scale" : 1 } 

i need delete subdocument, in case "component" has _id equals "52ebf4d971616b8716000011".

i've tried this:

db.slides.update({"components._id": objectid("52ebf4d971616b8716000011")}, {$pull: {"components._id": objectid("52ebf4d971616b8716000011"}}) 

but doesn't work, document remains same.

your attempted update doesn't work because you're trying pull components._id (which isn't array), instead of pulling components (which array) based on _id (which field you're trying pull documents).

you should change query to:

db.slides.update(   { 'components._id': some-id },    { $pull: { components: { _id: some-id } } });  

as @bryceatnetwork23 suggested.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -