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.