Reputation: 974
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()'));
However, if I replace count() with find() I get the following result:
Array
(
[retval] => Array
(
[value] => DBQuery: <dbName>.<collection>-> undefined
)
[ok] => 1
)
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.
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.
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.
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.
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?
Upvotes: 3
Views: 4128
Reputation: 46
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() }'));
My result:
Array
(
[retval] => Array
(
[count] => 10
)
[ok] => 1
)
Upvotes: 1
Reputation: 16297
$m = new MongoClient();
$db = $m-><dbName>;
print_r($db->execute('db.<collection>.toArray()'));
Suppose your databse name is test and collection name is foo .
print_r($m->test->execute('return db.foo.find().toArray()'));
Upvotes: 1