We have mentioned mongodb installation, configuration, cluster, and php insertion and update. For more information, see mongodb. Next let's talk about the test data {_ id: 1, title: A Dream of Red Mansions, auther: Cao Xueqin, typeColumn: test, money: 80, code: 10} {_ id: 2
We have mentioned mongodb installation, configuration, cluster, and php insertion and update. For more information, see mongodb. The following describes the test data for common operations of mongodb select: {"_ id": 1, "title": "Dream of Red Mansions", "auther": "Cao Xueqin", "typeColumn ": "test", "money": 80, "code": 10 }{ "_ id": 2
We have mentioned mongodb installation, configuration, cluster, and php insertion and update. For more information, see:Mongodb.
The following describes common mongodb select operations.
Test Data
{"_ Id": 1, "title": "Dream of Red Mansions", "auther": "Cao Xueqin", "typeColumn": "test", "money": 80, "code": 10} {"_ id": 2, "title": "besieged city", "auther": "Qian Zhongshu", "typeColumn": "test ", "money": 56, "code": 20} {"_ id": 3, "title": "Chao Fa Baidi City", "auther": "Li Bai ", "typeColumn": "test", "money": 30, "code": 30 }{ "_ id": 4, "title": "near wine ", "auther": "Li Bai", "money": 90, "code": 40}
1. Obtain the number of table entries.
> Db. books. count (); 4> db. books. find (). count (); 4> db. books. count ({auther: "Li Bai"}); 2> db. books. find ({money :{$ gt: 40, $ lte: 60 }}). count (); 1> db. books. count ({money :{$ gt: 40, $ lte: 60}); 1
The php code is as follows:
$ Collection-> count (); // result: 4 $ collection-> find ()-> count (); // result: 4 $ collection-> count (array ("auther" => ""); // result: 2 $ collection-> find (array ("money" => array ('$ gt' => 40, '$ lte' => 60)-> count (); // result: 1 $ collection-> count (array ("money" => array ('$ gt' => 40, '$ lte' => 60 ))); // result: 1
Tip: $ gt is greater than, $ gte is greater than or equal to, $ lt is less than, $ lte is less than or equal to, $ ne is not equal to, $ exists does not exist, $ in specified range, $ nin specified not in a certain range
2. Fetch a single piece of data
> Db. books. findOne ();{??????? "_ Id": 1 ,??????? "Title": "Dream of Red Mansions ",??????? "Auther": "Cao Xueqin ",??????? "TypeColumn": "test ",??????? "Money": 80 ,??????? "Code": 10}> db. books. findOne ({auther: "Li Bai "});{??????? "_ Id": 3 ,??????? "Title": "Chao Fa Baidi City ",??????? "Auther": "Li Bai ",??????? "TypeColumn": "test ",??????? "Money": 30 ,??????? "Code": 30}
The php code is as follows:
$ Collection-> findOne (); $ collection-> findOne (array ("auther" => "Li Bai "));
3, find? Snapshot cursor
> Db. books. find ({$ query: {auther: "Li Bai"}, $ snapshot: true}); {"_ id": 3, "title": "Chao Fa Badi City ", "auther": "Li Bai", "typeColumn": "test", "money": 30, "code": 30 }{ "_ id": 4, "title ": "Near wine", "auther": "Li Bai", "money": 90, "code": 40}
The php code is as follows:
/*** Note: * after we perform the find () operation and get the $ result cursor, the cursor is still dynamic. * In other words, after I find (), after the completion of my cursor loop, if any more qualified records are inserted into the collection, these records will also be obtained by $ result. */$ result = $ collection-> find (array ("auther" => "Li Bai")-> snapshot (); foreach ($ result as $ id = >$ value) {var_dump ($ value );}
4. Custom Column Display
> Db. books. find ({}, {"money": 0, "auther": 0}); // money and auther do not show {"_ id": 1, "title ": "Dream of Red Mansions", "typeColumn": "test", "code": 10 }{ "_ id": 2, "title": "besieged city", "typeColumn ": "test", "code": 20 }{ "_ id": 3, "title": "Chao Fa Baidi City", "typeColumn": "test", "code ": 30} {"_ id": 4, "title": "near wine", "code": 40}> db. books. find ({}, {"title": 1}); // only show the title column {"_ id": 1, "title ": "Dream of Red Mansions" }{ "_ id": 2, "title": "besieged city" }{ "_ id": 3, "title ": "Chao Fa Baidi City" }{ "_ id": 4, "title": "near wine"}/*** the money is between 60 and 100, the typecolumn and money columns must exist */> db. books. find ({money: {$ gt: 60, $ lte: 100 }}, {"typeColumn": 1, "money": 1}); {"_ id ": 1, "typeColumn": "test", "money": 80} {"_ id": 4, "money": 90}
The php code is as follows:
$ Result = $ collection-> find ()-> fields (array ("auther" => false, "money" => false )); // do not display the auther and money columns $ result = $ collection-> find ()-> fields (array ("title" => true )); // only display the title column/*** the value of money ranges from 60 to 100, the typecolumn and money columns must exist */$ where = array ('typecolumn' => array ('$ exists' => true ), 'money' => array ('$ exists' => true,' $ gte' => 60, '$ lte' => 100 )); $ result = $ collection-> find ($ where );
5. Paging
> Db. books. find (). skip (1 ). limit (1); // skip the first entry and take a {"_ id": 2, "title": "besieged city", "auther": "Qian Zhongshu ", "typeColumn": "test", "money": 56, "code": 20}
This mysql, limit, and offset is a bit similar. The php code is as follows:
$ Result = $ collection-> find ()-> limit (1)-> skip (1); // skip 1 record and retrieve 1 record
6. Sort
> Db. books. find (). sort ({money: 1, code:-1}); // 1 indicates descending order-1 indicates ascending order. Does the order of parameters affect the sorting order? {"_ Id": 3, "title": "Chao Fa Baidi City", "auther": "Li Bai", "typeColumn": "test", "money": 30, "code": 30 }{ "_ id": 2, "title": "besieged city", "auther": "Qian Zhongshu", "typeColumn": "test ", "money": 56, "code": 20} {"_ id": 1, "title": "Dream of Red Mansions", "auther": "Cao Xueqin", "typeColumn ": "test", "money": 80, "code": 10 }{ "_ id": 4, "title": "near wine", "auther ": "Li Bai", "money": 90, "code": 40}
The php code is as follows:
$result = $collection->find()->sort(array('code'=>1,'money'=>-1));
7. Fuzzy search
> Db. books. find ({"title":/city/}); // like '% str %' to query the data in the set {"_ id": 2, "title ": "besieged city", "auther": "Qian Zhongshu", "typeColumn": "test", "money": 56, "code": 20 }{ "_ id": 3, "title": "Chao Fa Baidi City", "auther": "Li Bai", "typeColumn": "test", "money": 30, "code": 30}> db. books. find ({"auther":/^ Li/}); // like 'str % 'to query the data in the set {"_ id": 3, "title ": "Chao Fa Baidi City", "auther": "Li Bai", "typeColumn": "test", "money": 30, "code": 30 }{ "_ id ": 4, "title": "near wine", "auther": "Li Bai", "money": 90, "code": 40}> db. books. find ({"auther":/ $/}); // like '% str' to query the data in the set {"_ id": 2, "title ": "besieged city", "auther": "Qian Zhongshu", "typeColumn": "test", "money": 56, "code": 20}> db. books. find ({"title": {$ regex: 'city', $ options: 'I '}}); // like '% str %' to query the data in the set {"_ id": 2, "title": "besieged city", "auther": "Qian Zhongshu ", "typeColumn": "test", "money": 56, "code": 20 }{ "_ id": 3, "title": "Chao Fa Baidi City ", "auther": "Li Bai", "typeColumn": "test", "money": 30, "code": 30}
The php code is as follows:
$ Param = array ("title" => new MongoRegex ('/city/'); $ result = $ collection-> find ($ param ); $ param = array ("auther" => new MongoRegex ('/^ Li/'); $ result = $ collection-> find ($ param ); $ param = array ("auther" => new MongoRegex ('/ $/'); $ result = $ collection-> find ($ param );
8, $ in and $ nin
> Db. books. find ({money :{$ in: [20, 30, 90] }}); // you can find data with money equal to 20, 30, and 90 {"_ id": 3, "title ": "Chao Fa Baidi City", "auther": "Li Bai", "typeColumn": "test", "money": 30, "code": 30 }{ "_ id ": 4, "title": "near wine", "auther": "Li Bai", "money": 90, "code": 40}> db. books. find ({auther: {$ in: [/^ Li/,/^ /]}); // search for data starting with Li and Qian {"_ id ": 2, "title": "besieged city", "auther": "Qian Zhongshu", "typeColumn": "test", "money": 56, "code ": 20} {"_ id": 3, "title": "Chao Fa Baidi City", "auther": "Li Bai", "typeColumn": "test", "money ": 30, "code": 30 }{ "_ id": 4, "title": "near wine", "auther": "Li Bai", "money": 90, "code": 40}
The php code is as follows:
$ Param = array ("money" => array ('$ in' => array (20, 30, 90); $ result = $ collection-> find ($ param ); foreach ($ result as $ id = >$ value) {var_dump ($ value );} $ param = array ("auther" => array ('$ in' => array (new MongoRegex ('/^ Li /'), new MongoRegex ('/^ /'); $ result = $ collection-> find ($ param); foreach ($ result as $ id => $ value) {var_dump ($ value );}
? 9, $ or
> Db. books. find ({$ or: [{money: 20}, {money: 80}]}); // search for data with money equal to 20, 80 {"_ id": 1, "title": "Dream of Red Mansions", "auther": "Cao Xueqin", "typeColumn": "test", "money": 80, "code": 10}
The php code is as follows:
$param = array('$or'=>array(array("money"=>20),array("money"=>80)));$result = $collection->find($param);foreach ($result as $id=>$value) { var_dump($value);}
? 10, distinct
> Db. books. distinct ('audio'); ["Cao Xueqin", "Qian Zhongshu", "Li Bai"]> db. books. distinct ('audio', {money: {$ gt: 60}); ["Cao Xueqin", "Li Bai"]
The php code is as follows:
$result = $curDB->command(array("distinct" => "books", "key" => "auther"));foreach ($result as $id=>$value) { var_dump($value);}$where = array("money" => array('$gte' => 60));$result = $curDB->command(array("distinct" => "books", "key" => "auther", "query" => $where));foreach ($result as $id=>$value) { var_dump($value);}
Write it here first, which is just some common operations of SELECT. Next, I will write a little more.
We have mentioned mongodb installation, configuration, cluster, and php insertion and update. For more information, see mongodb. The following describes the test data for common operations of mongodb select: {"_ id": 1, "title": "Dream of Red Mansions", "auther": "Cao Xueqin", "typeColumn ": "test", "money": 80, "code": 10 }{ "_ id": 2, "title": "besieged city", "auther": "Qian Zhongshu ", "typeColumn": "test", "money": 56, "code": 20 }{ "_ id": 3, "title": "Chao Fa Baidi City ", "auther": "Li Bai", "typeColumn": "test", "money": 30, [...]