2

I am using php and MongoDB, the opensource library that i am using makes an auto-generated code to make calls to mongodb. it is sort of an ODM layer.

Now when i am using embedded documents, in the parent document, i have to update the set of embedded documents, the Library makes a call using $unset, which as per Documentation sets the value at index to null and does not remove it.

thus now when i want to replace the embedded document set, i have to first remove($unset) the existing set, and then add new leaving me with the result like this:

Original:
{
    "parentDocument": {
        "parent_id": "1",
        "embeddedDocument": {
            "0": {
                "childValue": "0"
            },
            "1": {
                "childValue": "1"
            },
            "2": {
                "childValue": "2"
            }

        }
    }
}

Updated:

{
    "parentDocument": {
        "parent_id": "1",
        "embeddedDocument": {
            "0": null,
            "1": null,
            "2": {
                "childValue": "2"
            }

        }
    }
}

How can i clean this data from the db..?? I have tried many forums, didnt find any valid solution for the same. I need to clean this complete data. Thanks

1 Answer 1

1

You should have used $pull instead of $unset.

  • $unset will set elements to null inside arrays

  • $pull will remove elements from arrays

You can remove null elements using $pull:{'a.b':null}

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

5 Comments

I tried $pull, works on array elements only. which means, that i remove the complete set first and then add back the elements that i wish to keep. Not a valid option...Or am i missing something
I will check the last line again...might be useful...will revert soon
Yes, indeed it just works with arrays. Is there a reason why you didn't use an array? "0", "1" and "2" as keys does look like an array could make sense. ... for normal docs and subdocs $unset should remove the key.
The embedded set is an array, that is correct, but the elements of the array have to be removed individually. that means value[0],value[1],value[2] to be removed and to be left with value[3] at the 0th index. but right now it remains at 3rd index and previous values are null
If it is an array, it should be written [{"childValue": "0"},{"childValue": "1"},{"childValue": "2"}]. Now the question is how you'd like to identify the elements to remove. If you need to remove it by array index, the only way is to use $unset and then $pull all null elements ( stackoverflow.com/questions/4588303/… ). If you can remove the elements by some known value, you can just use $pull ( stackoverflow.com/questions/16959099/… )

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.