将新数据插入集合mongodb php

时间:2018-05-09 15:55:20

标签: php database mongodb

在互联网上搜索到各处后,我仍然无法弄清楚如何在不删除现有数据的情况下在我的数据库集合中插入新数据。到目前为止编写的代码如下:

$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->delete([]);
$bulk -> insert(['accessToken'=>'PQRST','ID'=>'789']);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
        $connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
        try {
            $result = $connection->executeBulkWrite('db.collection', $bulk,$writeConcern);
        } catch (MongoDB\Driver\Exception\BulkWriteException $e) {
            $result = $e->getWriteResult();
            if ($writeConcernError = $result->getWriteConcernError()) {
                printf("%s (%d): %s\n",
                $writeConcernError->getMessage(),
                $writeConcernError->getCode(),
                var_export($writeConcernError->getInfo(), true)
                ); 
            }

            // Check if any write operations did not complete at all
            foreach ($result->getWriteErrors() as $writeError) {
                printf("Operation#%d: %s (%d)\n",
                $writeError->getIndex(),
                $writeError->getMessage(),
                $writeError->getCode()
                );
            }
        } catch (MongoDB\Driver\Exception\Exception $e) {
            printf("Other error: %s\n", $e->getMessage());
            exit;
        }
希望你能帮助我。感谢

0 个答案:

没有答案