get mongodb _id object after upsert with php

后端 未结 6 1042
我寻月下人不归
我寻月下人不归 2020-12-14 11:56

is it possible to get the new/updated _id after the query? example code:

$key = array( \'something\' => \'unique\' );
$data = array( \'$inc\' => array(         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 12:17

    You can also set fsync to true in an update/upsert, to get the _id returned to the object that has been passed to the update.

    $save = array ('test' => 'work');
    $m->$collection->update(criteria, $save, array('fsync' => true, 'upsert' => true));
    echo $save['_id']; //should have your _id of the obj just updated.
    

提交回复
热议问题