2016-11-22 100 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你可以做的插入,更新和与一个或一个以上的文件删除操作。