MongoDB Group Operation example in PHP _mongodb

Source: Internet
Author: User
Tags mongodb mongodb select php code prev

In the next chapter, this article mainly said, MongoDB group function, do or quite powerful, quite for find (), skip (), distinct (), etc., the use of more complex.

Test data:

Copy Code code as follows:

> Db.fruit.find ();
{"_id": 1, "category": "Fruit", "name": "Apple"}
{"_id": 2, "category": "Fruit", "name": "Peach"}
{"_id": 3, "category": "Fruit", "name": "Banana"}
{"_id": 4, "category": "Veggie", "name": "Corn"}
{"_id": 5, "category": "Veggie", "name": "Broccoli"}

1, according to category group

Copy Code code as follows:

> Db.fruit.group (
{
Key: {category:1},
Reduce:function (obj, prev) {
Prev.items.push (Obj.name);
},
Initial: {items: []}
}
);
[
{
"Category": "Fruit",
"Items": [
"Apple",
"Peach",
"Banana"
]
},
{
"category": "Veggie",
"Items": [
"Corn",
"Broccoli"
]
}
]

The PHP code is as follows:

Copy Code code as follows:

$keys = Array ("category" => 1);
$initial = Array ("Items" => array ());
$reduce = "function (obj, prev) {Prev.items.push (obj.name);}";
$g = $collection->group ($keys, $initial, $reduce);

Print_r ($G); The results are as follows.

Array
(
[retval] => Array
(
[0] => Array
(
[Category] => fruit
[Items] => Array
(
[0] => Apple
[1] => Peach
[2] => Banana
)

)

[1] => Array
(
[Category] => veggie
[Items] => Array
(
[0] => Corn
[1] => broccoli
)

)

)

[Count] => 5
[Keys] => 2
[OK] => 1
)

2, according to category to group, and statistics count

Copy Code code as follows:

> Db.fruit.group (
{
Key: {category:1},
Cond: {_id: {$gt: 2}},
Reduce:function (obj, prev) {
Prev.items.push (Obj.name);
prev.count++;
},
Initial: {items: [], count:0}
}
);
[
{
"Category": "Fruit",
"Items": [
"Banana"
],
"Count": 1
},
{
"category": "Veggie",
"Items": [
"Corn",
"Broccoli"
],
"Count": 2
}
]

The PHP code is as follows:

Copy Code code as follows:

$keys = Array ("category" => 1);
$initial = Array ("Items" => array (), ' count ' =>0);
$reduce = "function (obj, prev) {".
"Prev.items.push (Obj.name);".
"prev.count++;"
"}";
$condition = Array (' condition ' => array ("_id" => Array (' $GT ' => 2));
$g = $collection->group ($keys, $initial, $reduce, $condition);

Print_r ($G); The results are as follows.

Array
(
[retval] => Array
(
[0] => Array
(
[Category] => fruit
[Items] => Array
(
[0] => Banana
)

[Count] => 1
)

[1] => Array
(
[Category] => veggie
[Items] => Array
(
[0] => Corn
[1] => broccoli
)

[Count] => 2
)
)

[Count] => 3
[Keys] => 2
[OK] => 1
)

3, the use of Aggregate group function, but also very powerful

Copy Code code as follows:

> Db.fruit.aggregate ([
{$match: {_id: {$gt: 0}}},
{$group: {_id: "$category", Count: {$sum: 1}}},
{$sort: {count:-1}}}
]);
{"_id": "Fruit", "Count": 3}
{"_id": "Veggie", "Count": 2}

The PHP code is as follows:

Copy Code code as follows:

$cond = Array (
Array
' $match ' => array (' _id ' => array (' $GT ' => 0)),
),
Array
' $group ' => Array (
' _id ' => ' $category ',
' Count ' => array (' $sum ' => 1),
),
),
Array
' $sort ' => Array ("Count" =>-1),
),
);
$result = $collection->aggregate ($cond);
Print_r ($result); The results are as follows:

Array
(
[Result] => Array
(
[0] => Array
(
[_id] => fruit
[Count] => 3
)

[1] => Array
(
[_id] => Veggie
[Count] => 2
)

)

[OK] => 1
)

There are a lot of MongoDB select operations, here, just a few common features.

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.