1

so I got this embedded structure which looks something like

{
    "_id" : ObjectId("529246fe6803fa2f1c16b7a7"),
    "title" : "presentation 0",
    "description" : "It's an awesome presentation about presentations, dawg!",
    "timestamp" : " 20:32:03",
    "slides" : [ 
        {
            "_id" : ObjectId("529246fe6803fa2f1c16b7a8"),
            "elements" : [ 
                {
                    "_id" : ObjectId("529246fe6803fa2f1c16b7a9"),
                    ...
                },
                ...
       },
       ...
}

Where my root elements are presentations.

Now I want to be able to remove slides and elements using their ids.

I use

$result = $collection->update(
        array('_id' => $presentationId),
        array('$pull' => array('slides' => array('_id' => $slideId))
        ));

to remove slides, which works.

I tried

$result = $collection->update(
        array('_id' => $presentationId),
        array('$pull' => array('slides.elements' => array('_id' => $elementId))
        ));

to remove elements, but that didn't work...

Thanks!

0

1 Answer 1

3

To pull a slide from presentation your syntax should be

db.coll.update({_id:presId},{$pull:{"slides._id": slidesId}}

To pull an element from a slide you need to match appropriate slide and use positional operator:

db.coll.update({ "_id": presId, "slides._id":slidesId},
{$pull: {"slides.$.elements":{"_id":elementId"}}})

I would test the second one - some operations on embedded arrays within arrays are not allowed.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks! in PHP it's db.coll.update({ _id: presId, "slides._id":slidesId}, {$pull: {"slides.$.elements":{"slides.elements._id":elementId"}}})
Isn't that what I said? :)
oh sorry, I meant in PHP it's db.coll.update({ _id: presId, "slides._id":slidesId}, {$pull: {"slides.$.elements":{"_id":elementId"}}})
oh doh, of course you're right - I forgot I was already including the first part of the field on the left side... I will edit to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.