Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

mongocommandcursor.createfromdocument(3) [php man page]

MONGOCOMMANDCURSOR.CREATEFROMDOCUMENT(3)				 1				  MONGOCOMMANDCURSOR.CREATEFROMDOCUMENT(3)

MongoCommandCursor::createFromDocument - Create a new command cursor from an existing command response document

SYNOPSIS
publicstatic MongoCommandCursor MongoCommandCursor::createFromDocument (MongoClient $connection, string $hash, array $document) DESCRIPTION
Use this method if you have a raw command result with cursor information in it. Note that cursors created with this method cannot be iter- ated multiple times, as they will lack the original command necessary for re-execution. PARAMETERS
o $connection - Database connection. o $hash - The connection hash, as obtained through the third by-reference argument to MongoDB::command. o $document - Document with cursor information in it. This document needs to contain the id, ns and firstBatch fields. Such a document is obtained by calling the MongoDB::command with appropriate arguments to return a cursor, and not just an inline result. See the example below. RETURN VALUES
Returns the new cursor. EXAMPLES
Example #1 MongoCommandCursor.createFromDocument(3) <?php $m = new MongoClient; $d = $m->demo; // Define the aggregation pipeline $pipeline = [ [ '$group' => [ '_id' => '$country_code', 'timezones' => [ '$addToSet' => '$timezone' ] ] ], [ '$sort' => [ '_id' => 1 ] ], ]; // Execute the command. The "cursor" option instructs the server to return // cursor information in the response instead of inline results. $r = $d->command( [ 'aggregate' => 'cities', 'pipeline' => $pipeline, 'cursor' => [ 'batchSize' => 1 ], ], null, $hash ); // Show result and hash var_dump( $r, $hash ); // Construct the command cursor $cursor = MongoCommandCursor::createFromDocument( $m, $hash, $r ); ?> The above example will output something similar to: array(2) { ["cursor"]=> array(3) { ["id"]=> object(MongoInt64)#5(1) { ["value"]=> string(12) "392143983421" } ["ns"]=> string(11) "demo.cities" ["firstBatch"]=> array(1) { [0]=> array(2) { ["_id"]=> string(2) "AD" ["timezones"]=> array(1) { [0]=> string(14) "Europe/Andorra" } } } } ["ok"]=> float(1) } string(25) "localhost:27017;-;.;17617" As you can see, the returned cursor information has the id, ns and firstBatch fields. SEE ALSO
MongoCommandCursor.__construct(3). PHP Documentation Group MONGOCOMMANDCURSOR.CREATEFROMDOCUMENT(3)

Check Out this Related Man Page

MONGOCOLLECTION.FIND(3) 						 1						   MONGOCOLLECTION.FIND(3)

MongoCollection::find - Queries this collection, returning aMongoCursorfor the result set

SYNOPSIS
public MongoCursor MongoCollection::find ([array $query = array()], [array $fields = array()]) DESCRIPTION
PARAMETERS
o $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. Warning Please 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. o $fields - Fields of the results to return. The array is in the format array('fieldname' => true, 'fieldname2' => true). The _id field is always returned. RETURN VALUES
Returns a cursor for the search results. EXAMPLES
Example #1 MongoCollection.find(3) example This example demonstrates basic search options. <?php $m = new MongoClient(); $db = $m->selectDB('test'); $collection = new MongoCollection($db, 'produce'); // search for fruits $fruitQuery = array('Type' => 'Fruit'); $cursor = $collection->find($fruitQuery); foreach ($cursor as $doc) { var_dump($doc); } // search for produce that is sweet. Taste is a child of Details. $sweetQuery = array('Details.Taste' => 'Sweet'); echo "Sweet "; $cursor = $collection->find($sweetQuery); foreach ($cursor as $doc) { var_dump($doc); } ?> The above example will output: array(4) { ["_id"]=> object(MongoId)#7(1) { ["$id"]=> string(24) "50a87dd084f045a19b220dd6" } ["Name"]=> string(5) "Apple" ["Type"]=> string(5) "Fruit" ["Details"]=> array(2) { ["Taste"]=> string(5) "Sweet" ["Colour"]=> string(3) "Red" } } array(4) { ["_id"]=> object(MongoId)#8(1) { ["$id"]=> string(24) "50a87de084f045a19b220dd7" } ["Name"]=> string(5) "Lemon" ["Type"]=> string(5) "Fruit" ["Details"]=> array(2) { ["Taste"]=> string(4) "Sour" ["Colour"]=> string(5) "Green" } } Sweet: array(4) { ["_id"]=> object(MongoId)#7(1) { ["$id"]=> string(24) "50a87dd084f045a19b220dd6" } ["Name"]=> string(5) "Apple" ["Type"]=> string(5) "Fruit" ["Details"]=> array(2) { ["Taste"]=> string(5) "Sweet" ["Colour"]=> string(3) "Red" } } See MongoCursor for more information how to work with cursors. Example #2 MongoCollection.find(3) example This example demonstrates how to search for a range. <?php $m = new MongoClient(); $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); } ?> The above example will output: 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 #3 MongoCollection.find(3) example using $where This example demonstrates how to search a collection using javascript code to reduce the resultset. <?php $m = new MongoClient(); $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); } ?> The above example will output: array(3) { ["_id"]=> object(MongoId)#7(1) { ["$id"]=> string(24) "4ebc3e3710b89f2349000002" } ["name"]=> string(3) "Joe" ["age"]=> int(20) } Example #4 MongoCollection.find(3) example using $in This example demonstrates how to search a collection using the $in operator. <?php $m = new MongoClient(); $db = $m->selectDB('test'); $collection = new MongoCollection($db, 'phpmanual'); $cursor = $collection->find(array( 'name' => array('$in' => array('Joe', 'Wendy')) )); ?> The above example will output: array(3) { ["_id"]=> object(MongoId)#7(1) { ["$id"]=> string(24) "4ebc3e3710b89f2349000002" } ["name"]=> string(3) "Joe" ["age"]=> int(20) } Example #5 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(3) function. <?php $m = new MongoClient(); $db = $m->selectDB('test'); $collection = new MongoCollection($db, 'phpmanual'); $cursor = $collection->find(); $array = iterator_to_array($cursor); ?> The above example will output: 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(3) 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(3) (so that it will not try to use the non-existent _id values as keys). SEE ALSO
MongoCollection.findOne(3), MongoCollection.insert(3), MongoDB core docs on find.. PHP Documentation Group MONGOCOLLECTION.FIND(3)
Man Page