MongoDB Select common operation code example _php instance in PHP

Source: Internet
Author: User
Tags findone mongodb mongodb select

In front of the MongoDB installation, configuration, clustering, as well as PHP inserts and updates, please refer to: MongoDB.
Let's say, the common operations of MongoDB Select

Test data:

Copy Code code 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": A, "code": 20}
{"_id": 3, "title": "Chao fa Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "Money": "Code": 30}
{"_id": 4, "title": "Nearly Wine", "Auther": "Li Bai", "Money": "," Code ": 40}

1, take the number of tables

Copy Code code 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: $lte: 60}});
1

The PHP code is as follows, in order:

Copy Code code as follows:

$collection->count (); Results: 4
$collection->find ()->count (); Results: 4
$collection->count (Array ("Auther" => "Li Bai")); Results: 2
$collection->find (Array ("Money" =>array (' $gt ' =>40, ' $lte ' =>60))->count (); Results: 1
$collection->count ("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 equal, $ne is not equal, $exists does not exist, $in specified range, $nin specified not in a range

2, take a single data

Copy Code code as follows:

> Db.books.findOne ();
{
"_id": 1,
"title": "A Dream of Red mansions",
"Auther": "Cao Xueqin",
"Typecolumn": "Test",
"Money": 80,
"Code": 10
}

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

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

Copy Code code as follows:

$collection->findone ();
$collection->findone (Array ("Auther" => "Li Bai"));

3, find snapshot cursor

Copy Code code as follows:

> Db.books.find ({$query: {auther: "Li Bai"}, $snapshot: true});
{"_id": 3, "title": "Chao fa Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "Money": "Code": 30}
{"_id": 4, "title": "Nearly Wine", "Auther": "Li Bai", "Money": "," Code ": 40}

The PHP code is as follows:

Copy Code code as follows:

/**
Attention
* After we do the find () operation, we get the $result cursor, the cursor is still dynamic.
* In other words, after I find (), by the time my cursor loop completes, if the qualifying records are inserted into the collection, then the records will be $result.
*/
$result = $collection->find (Array ("Auther" => "Li Bai"))->snapshot ();
foreach ($result as $id => $value) {
Var_dump ($value);
}

4, custom column display

Copy Code code as follows:

> 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": "Fortress Besieged", "Typecolumn": "Test", "Code": 20}
{"_id": 3, "title": "Toward Hair Baidicheng", "Typecolumn": "Test", "code": 30}
{"_id": 4, "title": "Near Wine", "Code": 40}

> db.books.find ({},{"title": 1}); Show Title column only
{"_id": 1, "title": "Dream of Red Mansions"}
{"_id": 2, "title": "Fortress Besieged"}
{"_id": 3, "title": "Toward Hair 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 Code code as follows:

$result = $collection->find ()->fields (Array ("Auther" =>false, "Money" =>false)); Do not show auther and money columns

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

/**
*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, pagination

Copy Code code as follows:

> Db.books.find (). Skip (1). limit (1); Skip article, Take a
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": A, "code": 20}

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

Copy Code code as follows:

$result = $collection->find ()->limit (1)->skip (1);//skip 1 records, remove 1

6, sorting

Copy Code code as follows:

> Db.books.find (). Sort ({money:1,code:-1}); 1 indicates descending-1 for ascending, and the sequence of parameters affects the sort order
{"_id": 3, "title": "Chao fa Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "Money": "Code": 30}
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": A, "code": 20}
{"_id": 1, "title": "Red Mansions", "Auther": "Cao Xueqin", "Typecolumn": "Test", "money": "," Code ": 10}
{"_id": 4, "title": "Nearly Wine", "Auther": "Li Bai", "Money": "," Code ": 40}

The PHP code is as follows:

Copy Code code as follows:

$result = $collection->find ()->sort (Array (' Code ' =>1, ' money ' =>-1);

7, Fuzzy query

Copy Code code as follows:

> Db.books.find ({"title":/City/}); Like '%str% ' paste the data in the query collection
{"_id": 2, "title": "Fortress Besieged", "Auther": "Qian Zhongshu", "Typecolumn": "Test", "money": A, "code": 20}
{"_id": 3, "title": "Chao fa Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "Money": "Code": 30}

> Db.books.find ({"Auther":/^ Lee/}); Like ' str% ' paste the data in the query collection
{"_id": 3, "title": "Chao fa Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "Money": "Code": 30}
{"_id": 4, "title": "Nearly Wine", "Auther": "Li Bai", "Money": "," Code ": 40}

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

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

The PHP code is as follows, in order:

Copy Code code 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 Code code as follows:

> Db.books.find ({money: {$in: [20,30,90]}}); Find data with money equal to 20,30,90
{"_id": 3, "title": "Chao fa Baidicheng", "Auther": "Li Bai", "Typecolumn": "Test", "Money": "Code": 30}
{"_id": 4, "title": "Nearly Wine", "Auther": "Li Bai", "Money": "," Code ": 40}

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

The PHP code is as follows, in order:

Copy Code code 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 Code code as follows:

> Db.books.find ({$or: [{money:20}, {money:80}]}); Find data with money equal to 20, 80
{"_id": 1, "title": "Red Mansions", "Auther": "Cao Xueqin", "Typecolumn": "Test", "money": "," Code ": 10}

The PHP code is as follows:

Copy Code code as follows:

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

10, distinct

Copy Code code 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 Code code 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);
}

The

is written here, which is just some of the most common operations of select, followed by a little bit of writing.

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.