get mongodb _id object after upsert with php

后端 未结 6 1046
我寻月下人不归
我寻月下人不归 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:08

    Just in case someone stumbles across this question like I did, Mongo will actually modify the input array when you call MongoCollection->save(); - appending the id to the end. So, if you call:

    $test = array('test'=>'testing');
    mongocollection->save($test);
    echo $test['_id'];
    

    You will have the mongo id for that object.

提交回复
热议问题