如何在 PHP 中访问 MongoDB 配置文件? [英] How to access MongoDB profile in PHP?

查看:42
本文介绍了如何在 PHP 中访问 MongoDB 配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在 mongo 客户端中使用的相同查询访问 PHP 中的 MongoDB 分析器:

I'm trying to access the MongoDB profiler in PHP with the same query I would use in the mongo client:

$db = $mongo->selectDB('myapp_db');
$array = $db->execute('return db.system.profile.find();');
echo '<pre>' . print_r($array, true);
But I get this:

但我明白了:

Profiling is enabled and works fine in the client.

分析已启用并在客户端正常工作.

解决方案

推荐答案

当您可以使用 PHP 本身时,无需在 JavaScript 中执行查询,这会阻塞服务器/数据库:

$mongo = new MongoClient(); // Alternatively, use selectCollection from $mongo->myapp_db $collection = $mongo->selectCollection('myapp_db', 'system.profile'); foreach ($collection->find() as $document) { print_r($document); }

This makes more efficient use of memory, since you can iterate through results instead of fetching the entire MongoDB::execute() response in a single array.

这可以更有效地利用内存,因为您可以遍历结果而不是获取整个 MongoDB::execute() 单个数组中的响应.

Additionally, your original code returns the cursor (a DBQuery object) from JavaScript. To ensure compatibility with other drivers, you should invoke cursor.toArray() before returning. This is discussed in Sammaye's answer to a similar question here.

这篇关于如何在 PHP 中访问 MongoDB 配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆