4

I am on php 5.6 using WAMP and want to insert a document into MongoDB using PHP. I am doing it in this way:

<?php

   require 'vendor/autoload.php';

   $con = new MongoDB\Client("mongodb://localhost:27017");
   echo "successfully";

   $db = $con->selectDatabase('DB');
   echo "Selected";

   $col = $db->selectCollection('myCol');
   $document = array( 
      "name" => "Deny", 
      "password" => "1234"
   );

   $col->insert($document);
   echo "successfully";

   ?>

But it is giving the error

Fatal error: Call to undefined method MongoDB\Collection::insert() in C:\wamp64\www...

I have read http://php.net/manual/en/mongocollection.insert.php and when I use the same insert function, it doesn't work for me.

9
  • Are you using this lib mongodb.github.io/mongo-php-library/api/index.html ? Commented Oct 17, 2016 at 8:08
  • @kcp Any installation page link? Commented Oct 17, 2016 at 8:11
  • @kcp Is it this one? https://github.com/mongodb/mongo-php-library Commented Oct 17, 2016 at 8:13
  • Yes it is this one. Commented Oct 17, 2016 at 8:22
  • @kcp I have run the command composer require "mongodb/mongodb=^1.0.0" but what about .zip files? Commented Oct 17, 2016 at 10:46

3 Answers 3

1

Instead of MongoDB\Client use MongoClient .

This works for me.

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

Comments

0

You have to install MongoClient library: http://php.net/manual/en/mongo.installation.php

3 Comments

This extension is deprecated, better to use this: mongodb.github.io/mongo-php-library/api/index.html
Can you share installation page link?
@Kristiyan I am unable to install it. Can you help me?
0

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

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.