How to write find query in mongo db to select certain values. For example
IN MYSQL - SELECT * from things where id=3;
IN Mongo - thingscollection->find(array("_id" => $id))
suppose if the MYSQL query looks like this,
SELECT name,age from things where id=3;
I am wondering how to write find query in PHP/MongoDB to select specific values?
The $in operator allows you to write queries that will return documents with values matching one of multiple values held in an array. The following example query includes the $in operator, and will return documents whose name value matches either Everest or K2 : db. peaks.
An SQL SELECT statement typically retrieves data from tables in a database, much like a mongo shell find statement retrieves documents from a collection in a MongoDB database.
You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the two following components: The extension , which provides a low-level API and mainly serves to integrate libmongoc and libbson with PHP.
MySQL: SELECT name,age from things where id=3;
Mongo: $db->things->find(array("id" => 3), array("name" => 1, "age" => 1));
You may want to use the mongo _id
instead of an own created id
field.
For more information specifically about the php driver: http://php.net/manual/en/mongo.sqltomongo.php An other good link for just SQL to Mongo is http://rickosborne.org/download/SQL-to-MongoDB.pdf
use SQL to Mongo as a reference when writing queries in Mongo Statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With