PHP7 mongodb集合插入
我们已经安装了php mongo ext,然后也设置了编写器 在我的php代码中,我有:PHP7 mongodb集合插入,php,mongodb,Php,Mongodb,我们已经安装了php mongo ext,然后也设置了编写器 在我的php代码中,我有: require_once 'vendor/autoload.php'; $m = new MongoDB\Client("mongodb://host1,host2", array ('replicaSet' => 'host-set-01')); $document = array(....); $db->mycollection->insert($document); 它返回了
require_once 'vendor/autoload.php';
$m = new MongoDB\Client("mongodb://host1,host2", array ('replicaSet' => 'host-set-01'));
$document = array(....);
$db->mycollection->insert($document);
它返回了这个错误:
PHP致命错误:未捕获错误:调用未定义的方法MongoDB\Collection::insert()
但是在适配器的文件夹中,我确实看到了集合类Mongo/MongoCollection.php中的insert()
有人使用mongodb/适配器使用它吗?您链接到()的库中的客户端是
\MongoClient
,而不是mongodb\client
php类是\MongoCollection
,而不是错误提示的MongoDB\Collection
我认为您试图使用的是PHP扩展版本的Mongo,而不是链接到的库。您链接到的库()中的客户端是
\MongoClient
,而不是MongoDB\client
php类是\MongoCollection
,而不是错误提示的MongoDB\Collection
我认为您正在尝试使用PHP扩展版本的Mongo,而不是您链接到的库。尝试使用
insertOne()
或insertMany()
尝试改用insertOne()
或insertMany()
这可能会对您有所帮助:
$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.
$mongo=新的MongoDB\Driver\Manager(“mongodb://localhost:27017");
$bulk=new MongoDB\Driver\BulkWrite;
$doc=数组(
'id'=>newmongodb\BSON\ObjectID,#生成MongoID
“name”=>“harry”,
“年龄”=>14岁,
“滚动编号”=>1
);
$bulk->insert($doc);
$mongo->executeBulkWrite('schooldb.student',$bulk);#“schooldb”是数据库,“student”是集合。
在这里,通过使用BulkWriter,您可以对一个或多个文档执行插入、更新和删除操作。这可能对您有所帮助: $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. $mongo=新的MongoDB\Driver\Manager(“mongodb://localhost:27017"); $bulk=new MongoDB\Driver\BulkWrite; $doc=数组( 'id'=>newmongodb\BSON\ObjectID,#生成MongoID “name”=>“harry”, “年龄”=>14岁, “滚动编号”=>1 ); $bulk->insert($doc); $mongo->executeBulkWrite('schooldb.student',$bulk);#“schooldb”是数据库,“student”是集合。
在这里,使用BulkWriter,您可以对一个或多个文档执行插入、更新和删除操作。是否可以发布此问题的正确修复程序?是否可以发布此问题的正确修复程序?