我想下面这段代码,但不工作 - 无输出显示
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
var_dump($manager);
?>
也尝试过下面所示的另一种代码,但是这也不产生输出
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
?>
我究竟做错了什么?
$ 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中
第一个需要有MongoDB的安装和PHP脚本“供应商/ autoload.php”到它的链接。在很多情况下,你需要检查你的主机:
$DB_CONNECTION_STRING="mongodb://YourCredentials";
require '../../vendor/autoload.php';
$manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );
然后,如果你使用的MongoDB \驱动程序\经理,MongoDB的驱动的现代版,你的代码看起来像CRUD操作的测试如下例子:
创建集合在一个文档:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$doc = ['name' => 'John', age => 33, profession => 'Guess what?'];
$bulkWrite->insert($doc);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);
按名称与极限读取集合中的文件:
$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);
通过MongoDB的_id与极限读取集合中的文件:
$filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);
集合中更新文件:(了解更多关于选择UPSERT和多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);
集合中删除文件 - 删除:
$bulkWrite=new MongoDB\Driver\BulkWrite;
$filter= ['name' => 'John', age => 35];
$options = ['limit' => 1];
$bulkWrite->delete($filter, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);