Eamorr,
The $ pull statement will not work with the document that you are using, because notifications are not an array. It is rather an embedded document with numbered keys, which makes it look like an array. There is no way (I know) to save this document structure and automatically rename the numbered keys.
If you are reorganizing your document a bit, look like this:
{ "notifications": [ { "type": "privateMessage", "fromUname": "Eamorr2", "time": 1292773522, "id": "1lfw70h789u13a1e67pv" }, { "type": "privateMessage", "fromUname": "Eamorr2", "time": 1292773522, "id": "iwoidjsoskqp23nlwof" } ], "toUname": "Eamorr" }
Elements will still be numbered, implicitly. Now it is an array, so you get it for free. You can use the $ pull operator like this (I am not familiar with the PHP driver, so I give you the shell equivalent):
db.messages.update({ "toUname" : "Eamorr" }, { $pull : { "notifications" : { "id" : "1lfw70h789u13a1e67pv" }}});
I arbitrarily used the "toUname" key to identify the document, but I think you'll want to use the _id field. In addition, I use the "id" -key for messages to identify the message that needs to be retrieved from the array, since it is much safer and ensures that you do not accidentally delete the wrong message in case of changing the array with you determined the sequence number of the array to delete.
I hope this helps.
source share