2016-11-22 86 views
0

我們已經安裝了php mongo ext,然後設置了作曲者。php 7 mongodb集合插入

https://github.com/alcaeus/mongo-php-adapter

在我的PHP代碼中,我這樣的:

require_once 'vendor/autoload.php'; 

$m = new MongoDB\Client("mongodb://host1,host2", array ('replicaSet' => 'host-set-01')); 
$document = array(....); 
$db->mycollection->insert($document); 

而且它返回此錯誤:

PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Collection::insert()

但是適配器的文件夾裏面我看到insert()該集合內class Mongo/MongoCollection.php

任何人都可以使用mongodb/adapter嗎?

+0

您能否爲此問題發佈正確的修補程序? – Nadeshwaran

回答

0

在您鏈接到(https://github.com/alcaeus/mongo-php-adapter)庫中的客戶端是\MongoClient,不MongoDB\Client

Mongo/MongoCollection.php類是\MongoCollection,而不是MongoDB\Collection,因爲您的錯誤提示。

我想你正在嘗試使用Mongo的PHP擴展版本,而不是你已經鏈接到的庫。

1

請嘗試使用insertOne()insertMany()代替!

1

這可能會幫助您:

 

    $mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");  
    $bulk = new MongoDB\Driver\BulkWrite; 

    $doc = array(
     'id'  => new MongoDB\BSON\ObjectID,  #Generate MongoID 
     'name' => 'harry', 
     'age'  => 14, 
     'roll_no' => 1 
    ); 

    $bulk->insert($doc); 
    $mongo->executeBulkWrite('schooldb.student', $bulk); # 'schooldb' is database and 'student' is collection. 

這裏使用BulkWriter你可以做的插入,更新和與一個或一個以上的文件刪除操作。