在PHP中执行MongoDB查找查询 [英] Executing MongoDB find queries in PHP
问题描述
我处于一种需要能够从PHP内部运行直接mongodb查询的情况,并且在执行execute()函数时遇到了麻烦.
I'm in a situation where I need to be able to run a direct mongodb query from inside of PHP, and am having troubles with the execute() function.
以下代码将正确执行并从数据库返回结果:
The following code will correctly execute and return a result from the database:
$m = new MongoClient();
$db = $m-><dbName>;
print_r($db->execute('db.<collection>.count()'));
但是,如果将count()替换为find(),则会得到以下结果:
However, if I replace count() with find() I get the following result:
Array
(
[retval] => Array
(
[value] => DBQuery: <dbName>.<collection>-> undefined
)
[ok] => 1
)
为什么其中一个查询有效,而另一个却失败?我该如何克服这个问题?我考虑过要编写一些东西,将MongoDB查询转换成必要的数组格式,以供PHP Mongo库使用,但这是我不想做的很多工作.
Why does one of these queries work, and the other fail? And how can I overcome this issue? I've thought about writing something that will convert a MongoDB query into the necessary array format for the PHP Mongo library to work with, but that would be a lot of work that I don't want to go through.
这是在这里出现的相同问题:如何访问MongoDB配置文件中使用PHP?-但是当时没有给出答案.
This is the same problem that was brought up here: How to access MongoDB profile in PHP? - but no answers were given at that time.
这个问题: PHP MongoDB execute()锁定集合利用了execute() find()的方法,他们说它适用于他们,但我不确定如何.
This question: PHP MongoDB execute() locking collection utilizes the execute() method with find() and they say it works for them, but I'm not sure how.
所以我用pecl更新了mongo,但这并不能解决任何问题.但是,我还进行了YUM更新,并且为php-pecl-mongo-1.2.12-1.el6.x86_64提供了单独的软件包更新,使我达到1.3.4-1.
So I'd updated mongo in pecl, but that didn't solve anything. However, I also did a YUM update, and that provided a separate package update for php-pecl-mongo-1.2.12-1.el6.x86_64 that brought me to 1.3.4-1.
现在,$db->execute('db.<collection>.find())
返回了一些信息……但根本没有达到我的期望.而不是返回MongoCursor
对象,而是返回了Array
,尽管它具有retval
字段,但是执行的查询中没有任何实际信息.看起来像这样:
Now, $db->execute('db.<collection>.find())
returns something...but not at all what I expect. Instead of returning a MongoCursor
object instead an Array
is returned, and while it has a retval
field, there's no actual information in there from the query performed. It looks like this:
Array
(
[retval] => Array
(
[_mongo] => Array
(
[slaveOk] =>
[host] => EMBEDDED
)
[_db] => Array
(
[_mongo] => Array
(
[slaveOk] =>
[host] => EMBEDDED
)
[_name] => test
)
[_collection] => Array
(
[_mongo] => Array
(
[slaveOk] =>
[host] => EMBEDDED
)
[_db] => Array
(
[_mongo] => Array
(
[slaveOk] =>
[host] => EMBEDDED
)
[_name] => test
)
[_shortName] => hits
[_fullName] => test.hits
)
[_ns] => test.hits
[_query] => Array
(
)
[_fields] =>
[_limit] => 0
[_skip] => 0
[_batchSize] => 0
[_options] => 0
[_cursor] =>
[_numReturned] => 0
[_special] =>
)
[ok] => 1
)
如您所见,数据库中实际上没有任何东西:如何获取我的实际行?
As you can see, there's not actually anything from the database there: how do I get to my actual rows?
推荐答案
我遇到了同样的问题,这是我使用execute的解决方案:
I had the same problem, this is my solution using execute:
$m = new MongoClient();
$db = $m-><dbName>;
print_r($db->execute('return { count : db.<collection>.count() }'));
我的结果:
Array
(
[retval] => Array
(
[count] => 10
)
[ok] => 1
)
这篇关于在PHP中执行MongoDB查找查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!