$coll_obyav->update(array( 'key' => $key ), array( 'key' => $key, '$addToSet' => array('obyav' => $info), , array('upsert' => true)); 

I want to update the document and insert elements into the array in one operation, but if the document does not exist, an error pops up because array field is not defined. We first have to findOne to check whether a document exists and then decide whether to add a new array or insert an element into an already existing array. Is there any better way to do this?

    0