开发者

Is there an equivalent to MySql's getLastInsertID() in MongoDb & PHP?

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 开发者_JS百科actually be in order.


$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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜