How can I upsert (update if not added) two fields at the same time?
It worked for a single field, but it doesn’t work for two anymore.
Am I doing something wrong?:
$db->url->update(
array("url" => $linkurlcache),
array('$set' => array('url' => $linkurlcache, 'pos' => $rand_num)),
array("upsert" => true),
array("multi" => true)
);
I really hate having to answer my own question. I would have deleted it but I couldn’t find a solution on any website.
Anyhow, the
array("multi" => true)made a lot of sense considering we are calling the update function, but it’s wrong.The correct solution is without it!
Just:
Because
upsertactually makes an insert (if the query doesn’t match).