An undocumented feature is that you can pass a stdClass instead of an array to $fields which will allow you to get around the issue of PHP casting string values to integers which mongo doesn't like. i.e.
array('123' => 0, '321' => 0);
becomes:
array(213 => 0, 312 => 0);
which will trip up with the error:
PHP Fatal error: Uncaught exception 'MongoException' with message 'field names must be strings'
The fix is as simple as:
<?php
$fields = new stdClass;
$fields->{123} = 0;
$fields->{321} = 0;
?>
MongoCollection::find
(PECL mongo >=0.9.0)
MongoCollection::find — Querys this collection, returning a MongoCursor for the result set
Descrierea
Parametri
-
query -
The fields for which to search. MongoDB's query language is quite extensive. The PHP driver will in almost all cases pass the query straight through to the server, so reading the MongoDB core docs on » find is a good idea.
AvertizarePlease make sure that for all special query operators (starting with $) you use single quotes so that PHP doesn't try to replace "$exists" with the value of the variable $exists.
-
fields -
Fields of the results to return. The array is in the format array('fieldname' => true, 'fieldname2' => true). The _id field is always returned.
Valorile întoarse
Returns a cursor for the search results.
Exemple
Example #1 MongoCollection::find() example
This example demonstrates how to search for a range.
<?php
$m = new Mongo();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
// search for documents where 5 < x < 20
$rangeQuery = array('x' => array( '$gt' => 5, '$lt' => 20 ));
$cursor = $collection->find($rangeQuery);
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
Exemplul de mai sus va afișa:
array(2) {
["_id"]=>
object(MongoId)#10 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000000"
}
["x"]=>
int(12)
}
array(2) {
["_id"]=>
object(MongoId)#11 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000001"
}
["x"]=>
int(12)
}
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
$m = new Mongo();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$js = "function() {
return this.name == 'Joe' || this.age == 50;
}";
$cursor = $collection->find(array('$where' => $js));
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
Exemplul de mai sus va afișa:
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
Example #3 MongoCollection::find() example using $in
This example demonstrates how to search a collection using the $in operator.
<?php
$m = new Mongo();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$cursor = $collection->find(array(
'name' => array('$in' => array('Joe', 'Wendy'))
));
?>
Exemplul de mai sus va afișa:
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
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
$m = new Mongo();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$cursor = $collection->find();
$array = iterator_to_array($cursor);
?>
Exemplul de mai sus va afișa:
array(3) {
["4ebc40af10b89f5149000000"]=>
array(2) {
["_id"]=>
object(MongoId)#6 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000000"
}
["x"]=>
int(12)
}
["4ebc40af10b89f5149000001"]=>
array(2) {
["_id"]=>
object(MongoId)#11 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000001"
}
["x"]=>
int(12)
}
["4ebc40af10b89f5149000002"]=>
array(3) {
["_id"]=>
object(MongoId)#12 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
}
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).
Vedeți de asemenea
- MongoCollection::findOne() - Querys this collection, returning a single element
- MongoCollection::insert() - Inserts an array into the collection
- MongoDB core docs on » find.
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
