note 101316 added to mongocollection.find

From: Date: Thu, 09 Dec 2010 14:28:39 +0000
Subject: note 101316 added to mongocollection.find
Groups: php.notes 
Request: Send a blank email to php-notes+get-174739@lists.php.net to get a copy of this message
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 ---- Server IP: 89.31.146.176 Probable Submitter: 85.170.31.105 ---- Manual Page -- http://www.php.net/manual/en/mongocollection.find.php Edit -- https://master.php.net/note/edit/101316 Del: integrated -- https://master.php.net/note/delete/101316/integrated Del: useless -- https://master.php.net/note/delete/101316/useless Del: bad code -- https://master.php.net/note/delete/101316/bad+code Del: spam -- https://master.php.net/note/delete/101316/spam Del: non-english -- https://master.php.net/note/delete/101316/non-english Del: in docs -- https://master.php.net/note/delete/101316/in+docs Del: other reasons-- https://master.php.net/note/delete/101316 Reject -- https://master.php.net/note/reject/101316 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174739) next »