创建一个文档:
如何获取新文档的ID?
$db->collection->insert($content);
// $newDocID = ???
如何获取新文档的ID?
$db->collection->insert($content);
// $newDocID = ???
在插入之前,您也可以获取_id。只需将_id字段添加到带有新MongoId的文档中即可。
$content['_id'] = new MongoId();
$db->collection->insert($content);
此外,这样做还有很多好处:
E11000重复键错误索引
。 - Somnath Muluk对我来说这个方法有效:
$insertResult = $collection->insertOne($object);
$id = $insertResult->getInsertedId();
getInsertedId
方法将返回一个\MongoDB\BSON\ObjectID
实例。要获取实际的ID值,请将其转换为字符串:(string)$insertResult->getInsertedId()
- Luke $newDocument = $db->collection->findAndModify ( $row, $row, null, array('new'=>true,'upsert' => true));
$strId = $newDocument['_id']->{'$id'};
fsync
选项:$db->collection->insert($content, 'fsync' => \TRUE);
这将确保“事务”先完成... - ZagNut