Mongodb php get id of new document?

Mark picture Mark · Dec 24, 2010 · Viewed 31.6k times · Source

Creating a document:

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

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

Answer

Theo picture Theo · Dec 24, 2010

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

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