0

We've installed php mongo ext and then have the composer set up as well.

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

In my php code I've this:

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()

But inside the adapter's folder I do see insert() inside that collection class Mongo/MongoCollection.php

Anyone got it working with the mongodb/adapter?

1
  • Can you please post the correct fix for this issue? Commented Nov 20, 2017 at 7:26

3 Answers 3

4

Try using insertOne() or insertMany() instead!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

The client in the library that you link to (https://github.com/alcaeus/mongo-php-adapter) is \MongoClient, not MongoDB\Client.

The Mongo/MongoCollection.php class is \MongoCollection, not MongoDB\Collection as your error suggests.

I think you're trying to use a PHP extension version of Mongo and not the library that you have linked to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.