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.
Instead of method MongoDB\Collection::insert()
using insertOne()
or insertMany()
would work!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With