1

I tried this code below, but not working - No Output Showing

<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
var_dump($manager);
?>

Also tried another code shown below, but this also produces no output

<?php
   // connect to mongodb
   $m = new MongoClient();

   echo "Connection to database successfully";
   // select a database
   $db = $m->mydb;

   echo "Database mydb selected";
?>

What am i doing wrong?

$ mongod --version
db version v3.4.4
git version: 888390515874a9debd1b6c5d36559ca86b44babd
OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
allocator: tcmalloc
modules: none
build environment:
distmod: ubuntu1404
    distarch: x86_64
    target_arch: x86_64

**

$ php --version
PHP 5.6.30-12~ubuntu14.04.1+deb.sury.org+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

**

$ php -i | grep extension_dir
extension_dir => /usr/lib/php/20131226 => /usr/lib/php/20131226
$ cd /usr/lib/php/20131226
$ ls
calendar.so  exif.so      gettext.so  mbstring.so  pdo.so    readline.so   sockets.so  sysvshm.so    xmlreader.so  xsl.so
ctype.so     fileinfo.so  iconv.so    mongodb.so   phar.so   shmop.so      sysvmsg.so  tokenizer.so  xml.so
dom.so       ftp.so       json.so     opcache.so   posix.so  simplexml.so  sysvsem.so  wddx.so       xmlwriter.so

**

echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n";

produces output - not loaded

php.ini

enter image description here

1 Answers1

0

First one need to have mongodb installed and a link to it in php script 'vendor/autoload.php'. In many cases you need to check your hosting:

  $DB_CONNECTION_STRING="mongodb://YourCredentials";
  require '../../vendor/autoload.php';
  $manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );

Then if you use MongoDB\Driver\Manager, a modern version of MongoDB driver, you code will look like the following tested examples of CRUD operations:

Create a document in the collection:

$bulkWrite = new MongoDB\Driver\BulkWrite;
$doc = ['name' => 'John', age => 33, profession => 'Guess what?'];
$bulkWrite->insert($doc);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);

Read document in the collection by name with a limit:

$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);

Read document in the collection by MongoDb _id with a limit:

$filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);    

Update document in the collection: (Read more about options upsert and multi here)

$bulkWrite=new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John'];
$update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
$options = ['multi' => false, 'upsert' => false];
$bulkWrite->update($filter, $update, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);    

Delete document in the collection - Delete:

$bulkWrite=new MongoDB\Driver\BulkWrite;
$filter= ['name' => 'John', age => 35];
$options = ['limit' => 1];
$bulkWrite->delete($filter, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);
Roman
  • 19,236
  • 15
  • 93
  • 97