Online Snippets Library

Mongo Query Reference

We are assuming that you have already connected to Mongodb.If you are not connected to Mongodb,Please have look our Post on PHP Connect to MongoDB.

Use limit in Mongodb collections

<?php
$result = $db->users->find()->limit(5); // find 5 record
//Regular query : SELECT * FROM users LIMIT 5;
foreach($result as $doc)
print_r($doc);
?>

Find two column from Mongodb collections

<?php
$result = $db->users->find(array(),array('column-1','column-2'));
//Regular query : SELECT column-1,column-2 FROM users;
foreach($result as $doc)
print_r($doc);
?>

Find With Where Clause in Mongodb collections

<?php
$result = $db->users->find(array("column-1"=>"tutsway"),array('column-1','column-2'));
//Regular query : SELECT column-1,column-2 FROM users Where column-1='tutsway';
foreach($result as $doc)
print_r($doc);
?>

Find With In Clause in Mongodb collections

<?php
$result = $db->users->find(array("column-1"=>array('$in' =>array('tutsway','tubeccone'))),array('column-1','column-2'));
//Regular query : SELECT column-1,column-2 FROM users Where column-1 IN ('tutsway','tubeccone');
foreach($result as $doc)
print_r($doc);
?>

Find With Not In Clause in Mongodb collections

<?php
$result = $db->users->find(array("column-1"=>array('$nin' =>array('tutsway','tubeccone'))),array('column-1','column-2'));
//Regular query : SELECT column-1,column-2 FROM users Where column-1 NOT IN ('tutsway','tubeccone');
foreach($result as $doc)
print_r($doc);
?>

Use Insert in Mongodb

<?php
$result = $db->users->insert(array("column-1" => 1, "column-2" => 2));
//Regular query : INSERT INTO USERS VALUES(1,2) ;
?>

Use Sort in Mongodb

<?php
$result = $db->users->find(array("column-1"=>"tutsway"),array('column-1','column-2'))->sort(array("column-1" => 1));
//Regular query : SELECT column-1,column-2 FROM users Where column-1='tutsway' ORDER BY column-1;
foreach($result as $doc)
print_r($doc);
?>

Use > Operator in Mongodb

<?php
$result = $db->users->find(array("column-1"=>array('$gt' => 33)),array('column-1','column-2'));
//Regular query : SELECT column-1,column-2 FROM users Where column-1 >'33';
foreach($result as $doc)
print_r($doc);
?>

Use < Operator in Mongodb

<?php
$result = $db->users->find(array("column-1"=>array('$lt' => 33)),array('column-1','column-2'));
//Regular query : SELECT column-1,column-2 FROM users Where column-1 < '33';
foreach($result as $doc)
print_r($doc);
?>

Find With Like in Mongodb collections

<?php
$result = $db->users->find(array("column-1" => new MongoRegex("/Joe/")),array('column-1','column-2'));
$result1 = $db->users->find(array("column-1" => new MongoRegex("^/Joe/")),array('column-1','column-2'));
//Regular query : SELECT column-1,column-2 FROM users Where column-1 LIKE "%Joe%"
//Regular query1 : SELECT column-1,column-2 FROM users WHERE column-1 LIKE "Joe%"  
?>

Find With Like in Mongodb collections

<?php
$result = $db->users->find(array("column-1" => new MongoRegex("/Joe/")),array('column-1','column-2'));
//Regular query : SELECT column-1,column-2 FROM users Where column-1 LIKE "%Joe%"
foreach($result as $doc)
print_r($doc);
?>

Use EXPLAIN in Mongodb

<?php
$result = $db->users->find(array("column-1"=>array('$gt' => 33)))->explain();
//Regular query : EXPLAIN SELECT * FROM users Where column-1 >'33';
foreach($result as $doc)
print_r($doc);
?>

DELETE Record from Mongodb collections

<?php
$result = $db->users->remove(array("column-1" => "abcd"));
//Regular query :  DELETE FROM users WHERE column-1="abcd";
?>

Use DESC in Mongodb

<?php
$result = $db->users->find()->sort(array("column-1" => -1));;
//Regular query : SELECT * FROM users ORDER BY column-1 DESC;
foreach($result as $doc)
print_r($doc);
?>

UPDATE Record from Mongodb collections

<?php
$result = $db->users->update(array("column-1" => "abcd"), array('$set' => array("column-2" => 1)));
//Regular query : UPDATE users SET a=1 WHERE column-1='abcd';
?>