简体   繁体   English

PHP 7的MongoDB集合插入

[英]php 7 mongodb collection insert

We've installed php mongo ext and then have the composer set up as well. 我们已经安装了php mongo ext,然后也设置了作曲家。

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

In my php code I've this: 在我的PHP代码中,我这样做:

require_once 'vendor/autoload.php';

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

And it returned this error: 它返回此错误:

PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\\Collection::insert() PHP致命错误:未捕获错误:调用未定义方法MongoDB \\ Collection :: insert()

But inside the adapter's folder I do see insert() inside that collection class Mongo/MongoCollection.php 但是在适配器的文件夹中,我确实在该集合类Mongo / MongoCollection.php中看到了insert()

Anyone got it working with the mongodb/adapter? 任何人都可以使用mongodb / adapter吗?

尝试改用insertOne()insertMany()

This might be help you: 这可能对您有帮助:

$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.

Here By using BulkWriter you can do insert,update and delete operation with one or more than one document. 在这里,通过使用BulkWriter,您可以对一个或多个文档进行插入,更新和删除操作。

The client in the library that you link to ( https://github.com/alcaeus/mongo-php-adapter ) is \\MongoClient , not MongoDB\\Client . 您链接到的库中的客户端( https://github.com/alcaeus/mongo-php-adapter )是\\MongoClient ,而不是MongoDB\\Client

The Mongo/MongoCollection.php class is \\MongoCollection , not MongoDB\\Collection as your error suggests. Mongo/MongoCollection.php类是\\MongoCollection ,而不是您的错误所建议的MongoDB\\Collection

I think you're trying to use a PHP extension version of Mongo and not the library that you have linked to. 我认为您尝试使用的是Mongo的PHP扩展版本,而不是已链接到的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM