Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined method MongoDB\Collection::insert() in [PATH] on line 20

Tags:

php

mongodb

I want to insert a json array into my MongoDB, that sounds a simple task. I use MongoDB 3.2, PHP 5.5 and XamppServer 32 bit. I have already installed PHP Driver for MongoDB and Composer. So I tried to run the code below:

require "vendor/autoload.php";

// create connection
$m = new MongoDB\Client();
// select a database
$db = $m->test;
// select a collection
$collection = $db->foo;

//insert to Database
$document = array( "title" => "Mongo Sample", "number" => 2 );
$collection->insert($document);

But on running, I got this error:

**Fatal error:** Call to undefined method MongoDB\Collection::insert() in C:\xampp\...\test.php on line 13

I can't figure out how to fix the problem.

like image 222
H. Ati Avatar asked Dec 13 '16 12:12

H. Ati


1 Answers

Instead of method MongoDB\Collection::insert() using insertOne() or insertMany() would work!

like image 65
H. Ati Avatar answered Oct 18 '22 07:10

H. Ati