开发者

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

开发者 https://www.devze.com 2023-03-03 19:55 出处:网络
The title asks it all. I have found an example that looked something like this: db.find(fields = {\"-id\"}).sort(\"-id\", -1).limit(X)

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

精彩评论

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