class' MongoDB \ Cient'未找到

时间:2016-08-19 12:50:29

标签: php mongodb

我使用以下代码创建与MongoDB服务器的连接,以便创建数据库和东西,但我不断获得class 'MongoDB\Cient' not found。我真的需要帮助。

<?php 
    $client = new MongoDB\Client('mongodb://localhost:27017');
    $db = $client->reach;
    $collection = $db->messages;
    $collection->insertOne($record);
?>

其中$record是一些记录。

此外,$mng = new MongoDB\Driver\Manager("mongodb://localhost:27017");连接,我可以使用它与任何已创建的数据库进行通信(使用命令行)。所以,我想问MongoDB\Driver\Manager是否可以替换MongoDB\Client

1 个答案:

答案 0 :(得分:2)

经过所有的斗争,这解决了这个问题......

使用composer进行安装:

$ composer require "mongodb/mongodb=^1.0.0"
OUTPUT:
    ./composer.json has been created
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Installing mongodb/mongodb (1.0.0)
    Downloading: 100%         

Writing lock file
Generating autoload files

用法:

<?php
require 'vendor/autoload.php'; // include Composer goodies

$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;

$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );

echo "Inserted with Object ID '{$result->getInsertedId()}'";
?>

这解决了我的问题。点击链接:

http://docs.php.net/manual/en/mongodb.tutorial.library.php