【问题标题】:Comparision between MongoClient and MongoDB. Why MongoClient is better?MongoClient 和 MongoDB 的比较。为什么 MongoClient 更好?
【发布时间】:2016-05-10 04:41:14
【问题描述】:

我不喜欢新的 mongo,MongoDB 需要 PHP7 中的多个库。

php 5 中的 MongoClient(已弃用)更加舒适和轻便!

我决定启动一个脚本并比较两个版本,结果非常令人惊讶:

MongoDB (PHP 7.0.2)

$client = new MongoDB\Client(
    'mongodb://root:password@localhost:port',
    ['readPreference' => 'secondaryPreferred']
);
$db = $client->selectDatabase('namedb');
$collection = $client->selectCollection('namedb', 'test');
$document   = $collection->findOne(['_id' => 'works']);
var_dump($document);
$time       = microtime();
$time       = explode(' ', $time);
$time       = $time[1] + $time[0];
$finish     = $time;
$total_time = round(($finish - $start), 4);
echo '<br><br>Page generated in ' . $total_time . ' seconds.';

Mongo/MongoClient (PHP 5.6.17)

$db = new MongoClient('mongodb://root:password@localhost:port');
$c  = $db->namedb->test;
$a  = $c->findOne(array("_id" => 'works'));
var_dump($a);
$time       = microtime();
$time       = explode(' ', $time);
$time       = $time[1] + $time[0];
$finish     = $time;
$total_time = round(($finish - $start), 4);
echo '<br><br>Page generated in ' . $total_time . ' seconds.';

还有其他人遇到过这个问题吗? 用新版的MongoDB没发现什么好处,都是问题!

【问题讨论】:

  • MongoDB 仅提供低级 API。如果你想要一个更高级别的 API,比如 MongoDB 驱动程序为其他语言提供的,你需要使用 MongoDB 的 PHPLib。查看有关 MongoDB 的 php 手册,phplib 链接在那里。
  • 另外你需要使用microtime(true)!如果没有将参数设置为true,您将返回一个字符串,这将在数学运算中得到错误的结果。
  • $start = microtime(true); ....your mongodb operations... $totaltime = microtime(true) - $start; 真的没必要爆炸什么的。
  • @CharlotteDunois 我已经安装并查看结果。
  • Github 问题将是提出此类问题的一个更有成效的地方......性能的回归仍然是回归,会有差异,但我相信维护者愿意无论如何都知道...

标签: php mongodb php-5.6 php-7


【解决方案1】:

MongoClient 是作为 PHP 扩展编写的本机驱动程序。

MongoDB 是使用不同 PHP 扩展的 PHP 代码。我认为新扩展更容易维护,因为它共享 PHP 以外的项目使用的 libmongoc 库。

将本机驱动程序与 PHP 库进行比较是不公平的比较。如果您想比较性能,您应该直接尝试一个本地驱动程序与另一个。

我找不到使用新版 MongoDB 的任何好处

首先,旧的MongoClient 扩展已被弃用。只有当您或其他人选择拿起并维护它时,它才会继续存在。尽管为同一任务使用两个相似的库会令人困惑(请参阅 mysql vs mysqli)。

MongoDB 旨在提供更高级别的抽象,与原生驱动程序相比,您可能会看到更频繁的功能更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多