The MongoDB Select common operation code example in PHP "reprint"

Source: Internet
Author: User
Tags findone mongodb select

In front of the MongoDB installation, configuration, clustering, and PHP insert and update, please refer to: MongoDB.
Here's a look at the common operations of MongoDB Select

Test data:

Copy CodeThe code is as follows:
{"_id": 1, "title": "Red Mansions", "Auther": "Cao Xueqin", "Typecolumn": "Test", "Money": "Code": 10}
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": "," "Code": 20}
{"_id": 3, "title": "Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "money": $, "code": 30}
{"_id": 4, "title": "Near Wine", "Auther": "Li Bai", "money": All, "code": 40}

1. Take the number of table strips

Copy CodeThe 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: ($lte:)}). Count ();
1

> Db.books.count ({money:{$gt: max, $lte: 60}});
1

The PHP code is as follows, in order:

Copy CodeThe code is as follows:
$collection->count (); Results: 4
$collection->find ()->count (); Results: 4
$collection->count (Array ("Auther" and "Li Bai")); Results: 2
$collection->find (Array ("Money" =>array (' $gt ' =>40, ' $lte ' =>60))->count (); Results: 1
$collection->count (Array ("Money" =>array (' $gt ' =>40, ' $lte ' =>60)); Results: 1

Tip: $GT is greater than, $GTE is greater than or equal, $lt is less than, $lte is less than or equal, $ne is not equal, $exists does not exist, $in specified range, $nin specified not in a range

2. Take a single piece of data

Copy CodeThe code is as follows:
> Db.books.findOne ();
{
"_id": 1,
"title": "Red Mansions",
"Auther": "Cao Xueqin",
"Typecolumn": "Test",
"Money": 80,
"Code": 10
}

> Db.books.findOne ({auther: "Li Bai"});
{
"_id": 3,
"title": "Towards the hair Baidicheng",
"Auther": "Li Bai",
"Typecolumn": "Test",
"Money": 30,
"Code": 30
}

The PHP code is as follows, in order corresponding to the

Copy CodeThe code is as follows:
$collection->findone ();
$collection->findone (Array ("Auther" and "Li Bai"));

3. Find Snapshot Cursors

Copy CodeThe code is as follows:
> Db.books.find ({$query: {auther: "Li Bai"}, $snapshot: true});
{"_id": 3, "title": "Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "money": $, "code": 30}
{"_id": 4, "title": "Near Wine", "Auther": "Li Bai", "money": All, "code": 40}

The PHP code is as follows:

Copy CodeThe code is as follows:
/**
Note
* After we have done the find () operation, the cursor is still dynamic after the $result cursor has been obtained.
* In other words, after I find (), to my cursor loop to complete this time, if there are more qualifying records are inserted into the collection, then these records will be $result obtained.
*/
$result = $collection->find (Array ("Auther" and "Li Bai")->snapshot ();
foreach ($result as $id = = $value) {
Var_dump ($value);
}

4. Custom Column Display

Copy CodeThe code is as follows:
> db.books.find ({},{"money": 0, "Auther": 0}); Money and auther do not show
{"_id": 1, "title": "Red Mansions", "Typecolumn": "Test", "Code": 10}
{"_id": 2, "title": "Fortress Besieged", "Typecolumn": "Test", "Code": 20}
{"_id": 3, "title": "Baidicheng", "Typecolumn": "Test", "code": 30}
{"_id": 4, "title": "Near Wine", "Code": 40}

> db.books.find ({},{"title": 1}); Show only title columns
{"_id": 1, "title": "Red Mansions"}
{"_id": 2, "title": "Siege"}
{"_id": 3, "title": "Baidicheng"}
{"_id": 4, "title": "Near Wine"}

/**
*money between 60 and 100, Typecolumn and money two columns must exist
*/
> Db.books.find ({money:{$gt: $, $lte: 100}},{"Typecolumn": 1, "Money": 1});
{"_id": 1, "Typecolumn": "Test", "Money": 80}
{"_id": 4, "Money": 90}

The PHP code is as follows, in order:

Copy CodeThe code is as follows:
$result = $collection->find ()->fields (Array ("Auther" =>false, "Money" =>false)); Auther and Money columns are not displayed

$result = $collection->find ()->fields (Array ("title" =>true); Show only title columns

/**
*money between 60 and 100, Typecolumn and money two columns must exist
*/
$where =array (' Typecolumn ' =>array (' $exists ' =>true), ' Money ' =>array (' $exists ' =>true, ' $gte ' =>60, ' $ LTE ' =>100));
$result = $collection->find ($where);

5. Paging

Copy CodeThe code is as follows:
> Db.books.find (). Skip (1). limit (1); Skip the article and take a
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": "," "Code": 20}

This mysql,limit,offset is a bit similar, the PHP code is as follows:

Copy CodeThe code is as follows:
$result = $collection->find ()->limit (1)->skip (1);//skip 1 records, remove 1 strips

6. Sorting

Copy CodeThe code is as follows:
> Db.books.find (). Sort ({money:1,code:-1}); 1 means descending-1 means ascending, and the order of the arguments affects the sort sequence
{"_id": 3, "title": "Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "money": $, "code": 30}
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": "," "Code": 20}
{"_id": 1, "title": "Red Mansions", "Auther": "Cao Xueqin", "Typecolumn": "Test", "Money": "Code": 10}
{"_id": 4, "title": "Near Wine", "Auther": "Li Bai", "money": All, "code": 40}

The PHP code is as follows:

Copy CodeThe code is as follows:
$result = $collection->find ()->sort (Array (' Code ' =>1, ' money ' =>-1);

7. Fuzzy Query

Copy CodeThe code is as follows:
> Db.books.find ({"title":/City/}); Like '%str% ' paste query data in a collection
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": "," "Code": 20}
{"_id": 3, "title": "Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "money": $, "code": 30}

> Db.books.find ({"Auther":/^ Li/}); Like ' str% ' paste query data in a collection
{"_id": 3, "title": "Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "money": $, "code": 30}
{"_id": 4, "title": "Near Wine", "Auther": "Li Bai", "money": All, "code": 40}

> Db.books.find ({"Auther":/Book $/}); Like '%str ' paste query data in a collection
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": "," "Code": 20}

> Db.books.find ({"title": {$regex: ' City ', $options: ' I '}}); Like '%str% ' paste query data in a collection
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": "," "Code": 20}
{"_id": 3, "title": "Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "money": $, "code": 30}

The PHP code is as follows, in order:

Copy CodeThe code is as follows:
$param = Array ("title" = = new Mongoregex ('/city/'));
$result = $collection->find ($param);

$param = Array ("Auther" = new Mongoregex ('/^ Lee/'));
$result = $collection->find ($param);

$param = Array ("Auther" = new Mongoregex ('/book $/'));
$result = $collection->find ($param);

8, $in and $nin

Copy CodeThe code is as follows:
> Db.books.find ({money: {$in: [20,30,90]}}); Find data that money equals 20,30,90
{"_id": 3, "title": "Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "money": $, "code": 30}
{"_id": 4, "title": "Near Wine", "Auther": "Li Bai", "money": All, "code": 40}

> Db.books.find ({auther: {$in: [/^ Lee/,/^ Money/]}); Find data that begins with Lee, money
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": "," "Code": 20}
{"_id": 3, "title": "Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "money": $, "code": 30}
{"_id": 4, "title": "Near Wine", "Auther": "Li Bai", "money": All, "code": 40}

The PHP code is as follows, in order:

Copy CodeThe 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 ('/^ Lee/'), New Mongoregex ('/^ money/')));
$result = $collection->find ($param);
foreach ($result as $id = = $value) {
Var_dump ($value);
}

9. $or

Copy CodeThe code is as follows:
> Db.books.find ({$or: [{money:20}, {money:80}]}); Find Money equals 20, 80 of data
{"_id": 1, "title": "Red Mansions", "Auther": "Cao Xueqin", "Typecolumn": "Test", "Money": "Code": 10}

The PHP code is as follows:

Copy CodeThe code is as follows:
$param = Array (' $or ' =>array ("=>20"), Array ("Money" =>80));
$result = $collection->find ($param);
foreach ($result as $id = = $value) {
Var_dump ($value);
}

10, distinct

Copy CodeThe code is as follows:
> db.books.distinct (' auther ');
["Cao Xueqin", "Qian Zhongshu", "Li Bai"]

> db.books.distinct (' Auther ', {money: {$gt: 60}});
["Cao Xueqin", "Li Bai"]

The PHP code is as follows:

Copy CodeThe 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" and "= $where)");
foreach ($result as $id = = $value) {
Var_dump ($value);
}

First write here, above is just a few of the common operations of Select, Next, will write a little.

The MongoDB Select common operation code example in PHP "reprint"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.