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

Fr_nkenstien
  • 1,923
  • 7
  • 33
  • 66

1 Answers1

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}

Benjamin M
  • 23,599
  • 32
  • 121
  • 201
  • 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 – Fr_nkenstien Apr 08 '15 at 07:34
  • I will check the last line again...might be useful...will revert soon – Fr_nkenstien Apr 08 '15 at 07:35
  • 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. – Benjamin M Apr 08 '15 at 07:56
  • 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 – Fr_nkenstien Apr 08 '15 at 08:31
  • 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 ( http://stackoverflow.com/questions/4588303/in-mongodb-how-do-you-remove-an-array-element-by-its-index ). If you can remove the elements by some **known value**, you can just use `$pull` ( http://stackoverflow.com/questions/16959099/how-to-remove-array-element-in-mongodb ) – Benjamin M Apr 08 '15 at 09:14