Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB/PHP.. How to write select queries?

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?

like image 991
user1518659 Avatar asked Sep 20 '12 10:09

user1518659


People also ask

Can we write queries in MongoDB?

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.

What is the select query in MongoDB?

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.

Can we use PHP and MongoDB together?

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.


2 Answers

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

like image 77
Yogesh Suthar Avatar answered Oct 05 '22 09:10

Yogesh Suthar


use SQL to Mongo as a reference when writing queries in Mongo Statements.

like image 31
Vamsi Krishna B Avatar answered Oct 05 '22 08:10

Vamsi Krishna B