PHP 5.4.33 Released

MongoCollection::find

(PECL mongo >=0.9.0)

MongoCollection::findQuerys this collection

Description

public MongoCursor MongoCollection::find ([ array $query = array() [, array $fields = array() ]] )

Parameters

query

The fields for which to search.

fields

Fields of the results to return.

Return Values

Returns a cursor for the search results.

Examples

Example #1 MongoCollection::find() example

This example demonstrates how to search for a range.

<?php

// search for documents where 5 < x < 20
$rangeQuery = array('x' => array( '$gt' => 5'$lt' => 20 ));

$cursor $collection->find($rangeQuery);

?>

See MongoCursor for more information how to work with cursors.

Example #2 MongoCollection::find() example using $where

This example demonstrates how to search a collection using javascript code to reduce the resultset.

<?php

$collection 
$db->my_db->articles;

$js "function() {
  return this.type == 'homepage' || this.featured == true;
}"
;
$articles $collection->find(array('$where' => $js));

?>

Example #3 MongoCollection::find() example using $in

This example demonstrates how to search a collection using the $in operator.

<?php

$collection 
$db->my_db->articles;
$articles $collection->find(array(
  
'type' => array('$in' => array('homepage''editorial'))
));

?>

Example #4 Getting results as an array

This returns a MongoCursor. Often, when people are starting out, they are more comfortable using an array. To turn a cursor into an array, use the iterator_to_array() function.

<?php

$cursor 
$collection->find();
$array iterator_to_array($cursor);

?>

Using iterator_to_array() forces the driver to load all of the results into memory, so do not do this for result sets that are larger than memory!

Also, certain system collections do not have an _id field. If you are dealing with a collection that might have documents without _ids, pass FALSE as the second argument to iterator_to_array() (so that it will not try to use the non-existent _id values as keys).

See Also

MongoDB core docs on » find.

add a note add a note

User Contributed Notes 3 notes

up
2
nospam at alexyves dot fr
3 years ago
This will work with versions >=1.5.3, please note that this is just a example of the way to use the or statement.

<?php
  $connection
= new Mongo();

 
$db = $connection->test;
 
$collection = $db->test;
 
// Clean the DB before the test.
 
$collection->drop();
 
$collection = $db->test;

 
$apple = array(
   
'fruit' => 'Apple',
   
'type' => 'Juice',
  );

 
$orange = array(
   
'fruit' => 'Orange',
   
'type' => 'Marmalade',
  );

 
$collection->insert($apple);
 
$collection->insert($orange);

 
// Basic find
 
$results = $collection->find(array('fruit' => 'Apple'));

  foreach(
$results as $result)
  {
    echo
sprintf("Fruit: %s, Type: %s%s", $result['fruit'], $result['type'], PHP_EOL);
  }
?>

Output:

Fruit: Apple, Type: Juice

Now an advanced search with "or" statement.

<?php
 
// Advanced find with "OR" note the double array.
  // if you use double quotes escape the or "\$or"
 
$results = $collection->find( array( '$or' => array( array('fruit' => 'Apple'), array('fruit' => 'Orange') ) ) );

  foreach(
$results as $result)
  {
    echo
sprintf("Fruit: %s, Type: %s%s", $result['fruit'], $result['type'], PHP_EOL);
  }
?>

Output:

Fruit: Apple, Type: Juice
Fruit: Orange, Type: Marmalade
up
1
Nanhe Kumar
10 months ago
<?php
$m
= new MongoClient();
$db = $m->selectDB('school');
$collection = new MongoCollection($db, 'student');
//Find where class=5
$where=array('class'=>5);
$cursor = $collection->find($where);

//Find where class !=5
$where=array('class' => array('$ne'=>5));
$cursor = $collection->find($where);

//Find where age >20
$where=array('age' => array('$gt'=>20));
$cursor = $collection->find($where);

//Find where age >=20
$where=array('age' => array('$gte'=>20));
$cursor = $collection->find($where);

//Find where age <20
$where=array('age' => array('$le'=>20));
$cursor = $collection->find($where);

//Find where age <=20
$where=array('age' => array('$lte'=>20));
$cursor = $collection->find($where);

//Finc where class=10 or marks=80
$where=array( '$or' => array( array(' class' =>10), array('marks'=>80) ) );
$cursor = $collection->find($where);
//Finc where class=12 AND marks=70
$where=array( '$and' => array( array(' class' =>12), array('marks'=>70) ) );
$cursor = $collection->find($where);

?>
up
1
artusdebenque at yahoo dot fr
10 months ago
For the fields parameter, the documentaion says: "The _id field is always returned".
Knowing that mongodb allows you to uncheck the _id field ("the _id field is the only field that you can explicitly exclude"; source: http://docs.mongodb.org/manual/reference/method/db.collection.find/#db.collection.find), I tried it with php and it works : you can exclude the _id field.

Example : the following fields parameter will exclude the field "_id"

$fields = array('timestamp' => true, 'rank' => true, '_id' => false);
To Top