根据官网提供的使用案例进行学习,下面是我的测试代码
<?php class Mongo_Driver{ public function insertOne(){ $user = array( 'first_name' => 'MongoDB', 'last_name' => 'Fan', 'tags' => array('developer','user') ); // Configuration $dbhost = 'localhost'; $dbname = 'test'; // Connect to test database $m = new Mongo("mongodb://$dbhost"); $db = $m->$dbname; // Get the users collection $c_users = $db->users; // Insert this new document into the users collection $c_users->save($user); } public function selectOne(){ // Configuration $dbhost = 'localhost'; $dbname = 'test'; // Connect to test database $m = new Mongo("mongodb://$dbhost"); $db = $m->$dbname; // Get the users collection $c_users = $db->users; // Find the user with first_name 'MongoDB' and last_name 'Fan' $user = array( 'first_name' => 'MongoDB', 'last_name' => 'Fan' ); $user = $c_users->findOne($user); var_dump($user); } public function selectByLimit(){ // Configuration $dbhost = 'localhost'; $dbname = 'test'; // Connect to test database $m = new Mongo("mongodb://$dbhost"); $db = $m->$dbname; // Get the users collection $c_users = $db->users; // Find the user with first_name 'MongoDB' and last_name 'Fan' $user = array( 'first_name' => 'MongoDB', 'last_name' => 'Fan' ); $result = $c_users->find($user)->limit(1); var_dump($result); } public function selectByMongoId(){ // Configuration $dbhost = 'localhost'; $dbname = 'test'; // Connect to test database $m = new Mongo("mongodb://$dbhost"); $db = $m->$dbname; // Get the users collection $c_users = $db->users; // Find the user with first_name 'MongoDB' and last_name 'Fan' $user = array( 'first_name' => 'MongoDB', 'last_name' => 'Fan' ); // This is only a string, this is NOT a MongoId $mongoid = '5488ff0f2a7cdfb80f000000'; // You will not find anything by searching by string alone $nothing = $c_users->find(array('_id' => $mongoid)); echo $nothing->count(); // This should echo 0 // THIS is how you find something by MongoId $realmongoid = new MongoId($mongoid); // Pass the actual instance of the MongoId object to the query $something = $c_users->find(array('_id' => $realmongoid)); echo $something->count(); // This should echo 1 } public function selectByWhere(){ // Configuration $dbhost = 'localhost'; $dbname = 'test'; // Connect to test database $m = new Mongo("mongodb://$dbhost"); $db = $m->$dbname; // Get the users collection $c_users = $db->users; // Instantiate dates for the range of the query $start = new MongoDate(strtotime('1971-01-01 00:00:00')); $end = new MongoDate(strtotime('2014-12-31 23:59:59')); // Now find documents with create_date between 1971 and 1999 $c_users->find(array("create_date" => array('$gt' => $start, '$lte' => $end))); } public function selectByQuery(){ // Connect to test database on localhost $m = new Mongo('mongodb://localhost'); $db = $m->test; // Get the users collection $c_things = $db->things; // Get a count of documents in the things collection $count_things = $c_things->count(); echo "There are $count_things documents in the things collection.\n"; // How many have the boolean property set to true? $count_things = $c_things->count(array('boolean' => true)); echo "There are $count_things true documents in the things collection.\n"; // How many have a string property set, regardless of value? $count_things = $c_things->count(array('string' => array('$exists' => true))); echo "There are $count_things documents with strings in the things collection.\n"; // How many have a list property with array values including "one" and "two"? $count_things = $c_things->count(array('list' => array('$in' => array('one','two')))); echo "There are $count_things documents with 'one' and 'two' as list array values in the things collection.\n"; // How many have a list property with array values not including 'three'? $count_things = $c_things->count(array('list' => array('$nin' => array('three')))); echo "There are $count_things documents not including the string 'three' in list array values in the things collection.\n"; // How many have include 'ever was' in the string property? Using a regular expression: $regex = new MongoRegex("/ever was/"); $count_things = $c_things->count(array('string' => $regex)); echo "There are $count_things documents including the string 'ever was' in string property in the things collection.\n"; } public function returningDocument(){ // Connect to test database on localhost $m = new Mongo('mongodb://localhost'); $db = $m->test; // Get the users collection $c_things = $db->things; // Find a document that includes 'ever was' in the string property using a regular expression: $regex = new MongoRegex("/ever was/"); $ever_was = $c_things->findOne(array('string' => $regex)); var_dump($ever_was); } public function selectByIndex(){ // Connect to test database on localhost $m = new Mongo('mongodb://localhost'); $db = $m->test; // Get the users collection $c_things = $db->things; } } $mongo = new Mongo_Driver(); //var_dump($mongo->insertOne()); var_dump($mongo->returningDocument()); //end
转载请注明:小Y » MongoDB使用测试