I created an array in my mongo document, so it looks something like this:
{ "_id" : ObjectId("4f59e19d0b7aab2903000004"), "details" : { x:1, y:2 } }
Then I try to push the new value to the array by doing:
db.users.update({"_id" : ObjectId("4f59e19d0b7aab2903000004")},{$push: {"details": {"z":3}}});
However, I get the error message:
Cannot apply $push/$pushAll modifier to non-array
It seems the only way I can really add information to an array is to use dot notation to add it, for example.
db.users.update({"_id" : ObjectId("4f59e19d0b7aab2903000004")},{"details.z": 3});
This seems to work, but when I have an array of about 30 values, it looks a little tedious.
Just for clarity, I use the lithium PHP framework, and not just enter them manually, so I could iterate over the array to add โdetailsโ. for every key, but I donโt think it is necessary. Is there something that I'm missing, why won't it insert values โโinto an array?
(My lithium code was as follows :)
User::update(array('$push'=>array('details'=>array('z'=>3))), array('_id'=>$id))
Thanks,
Dan