Mark Mark - 5 months ago 25
PHP Question

Mongodb php get id of new document?

Creating a document:

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


I'm trying to get the new document's id. How? Thanks.

Answer

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

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