在MongoDb和PHP中是否有等效的MySql getLastInsertID()?

问题描述:

The title asks it all. I have found an example that looked something like this:

db.find(fields = {"-id"}).sort("-id", -1).limit(X)

but that doesn't seem safe because that is assuming the ids will actually be in order.

标题要求全部。 我找到了一个看起来像这样的例子: p> db.find(fields = {“ - id”})。sort(“ - id”, - 1).limit(X) code> pre> \ n

但这似乎并不安全,因为假设id实际上是有序的。 p> div>

$item = array( ... );
$mongo_collection->insert($item);
$id = $item['_id'];

It'll add the ID to the $item array. Also note you can use an object instead of an array if it's your preference.