How do I do MongoDB console-style queries in PHP?

Posted by Zoe Boles on Stack Overflow See other posts from Stack Overflow or by Zoe Boles
Published on 2011-11-28T17:46:58Z Indexed on 2011/11/28 17:54 UTC
Read the original article Hit count: 271

Filed under:
|
|

I'm trying to get a MongoDB query from the javascript console into my PHP app. What I'm trying to avoid is having to translate the query into the PHP "native driver"'s format... I don't want to hand build arrays and hand-chain functions any more than I want to manually build an array of MySQL's internal query structure just to get data.

I already have a string producing the exact content I want in the Mongo console:

db.intake.find({"processed": {"$exists": "false"}}).sort({"insert_date": "1"}).limit(10);

The question is, is there a way for me to hand this string, as is, to MongoDB and have it return a cursor with the dataset I request?

Right now I'm at the "write your own parser because it's not valid json to kinda turn a subset of valid Mongo queries into the format the PHP native driver wants" state, which isn't very fun.

I don't want an ORM or a massive wrapper library; I just want to give a function my query string as it exists in the console and get an Iterator back that I can work with. I know there are a couple of PHP-based Mongo manager applications that apparently take console-style queries and handle them, but initial browsing through their code, I'm not sure how they handle the translation.

I absolutely love working with mongo in the console, but I'm rapidly starting to loathe the thought of converting every query into the format the native writer wants...

© Stack Overflow or respective owner

Related posts about php

Related posts about query