phpmongodbphp-mongodb

Mongodb php get id of new document?


Creating a document:

$db->collection->insert($content);
// $newDocID = ???

How to get the new document's id?


Solution

  • According to the docs the array you pass to insert will be amended with an _id field:

    $db->collection->insert($content);
    $newDocID = $content['_id'];