This article mainly introduces examples of common code for mongodbselect operations in php. This article also contains mongodb operation commands and PHP implementation code. If you need it, refer to the above section for mongodb installation, configuration, and cluster, for more information about php insertion and update, see mongodb.
The following describes common mongodb select operations.
Test data:
The Code is as follows:
{"_ 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. Number of table entries
The Code is as follows:
> 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:
The 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. retrieve a single piece of data
The Code is as follows:
> 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:
The Code is as follows:
$ Collection-> findOne ();
$ Collection-> findOne (array ("auther" => "Li Bai "));
3. find snapshot cursor
The Code is as follows:
> Db. books. find ({$ query: {auther: ""}, $ snapshot: true });
{"_ 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:
The 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" => "")-> snapshot ();
Foreach ($ result as $ id => $ value ){
Var_dump ($ value );
}
4. Custom Column Display
The Code is as follows:
> Db. books. find ({}, {"money": 0, "auther": 0}); // The money and auther values are not displayed.
{"_ 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 displays 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 value ranges from 60 to 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:
The Code is as follows:
$ Result = $ collection-> find ()-> fields (array ("auther" => false, "money" => false); // The auther and money columns are not displayed.
$ Result = $ collection-> find ()-> fields (array ("title" => true); // only display the title Column
/**
* The money value 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
The Code is as follows:
> Db. books. find (). skip (1). limit (1); // skip the first entry and obtain
{"_ 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:
The Code is as follows:
$ Result = $ collection-> find ()-> limit (1)-> skip (1); // skip 1 record and retrieve 1 record
6. Sorting
The Code is as follows:
> Db. books. find (). sort ({money: 1, code:-1}); // 1 indicates descending order-1 indicates ascending order, and the order of parameters is affected.
{"_ 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:
The Code is as follows:
$ Result = $ collection-> find ()-> sort (array ('code' => 1, 'money' =>-1 ));
7. Fuzzy search
The Code is as follows:
> Db. books. find ({"title":/city/}); // like '% str %' to query 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 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 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:
The 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
The Code is as follows:
> Db. books. find ({money: {$ in: [20, 30, 90]}); // search for 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:
The 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
The Code is as follows:
> Db. books. find ({$ or: [{money: 20}, {money: 80}]}); // you can find data with money equal to 20 or 80.
{"_ Id": 1, "title": "Dream of Red Mansions", "auther": "Cao Xueqin", "typeColumn": "test", "money": 80, "code": 10}
The php code is as follows:
The Code is as follows:
$ Param = array ('$ or' => array ("money" => 20), array ("money" => 80 )));
$ Result = $ collection-> find ($ param );
Foreach ($ result as $ id => $ value ){
Var_dump ($ value );
}
10. distinct
The Code is as follows:
> 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:
The 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.