1

I'm reading MongoDB::command docs, but they see quite poor to me. How can I use ::command to query a collection?

Lets say I have a collection of things and each thing has a path (an alpha-numerical string of IDs of other things joined with /). How would I query all the things that starts with /2e3r4t/?

Maybe

::command(["path" => "/^/2e3r4t//"])

?

1 Answer 1

1

MongoDB::command is used to send raw database commands to the server. For most common commands, you will find a wrapper in your language library.

Here, given your description, you need Collection::find instead. something like that (untested -- beware of typos):

$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'thing');

$regex = new MongoRegex("/^\/2e3r4t\//");
$collection->find(array('path' => $regex));

Amusingly enough, as of MongoDB 3.0.2, the find command is not documented, and apparently is not yet implemented as a DB command:

> db.runCommand({find: "w"})
{ "ok" : 0, "errmsg" : "find command not yet implemented" }

So, for this one, you will have to rely on the corresponding method of your driver.


EDIT: from a quick look at the sources, the find command was implemented between 3.1.0 and 3.1.1:

Sign up to request clarification or add additional context in comments.

1 Comment

@alexandernst As of 3.0.2 this is not yet supported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.