Home >Backend Development >PHP Tutorial >After mongodb executes the group again, how to fetch a thousand pieces of data and how to limit the operation? The code is as described below?

After mongodb executes the group again, how to fetch a thousand pieces of data and how to limit the operation? The code is as described below?

WBOY
WBOYOriginal
2016-12-01 00:26:001493browse

<code>$keys = array(
    "AREA" => 1
);
$initial = array(
    "show" =>0,
    "click"=>0,
    "cost"=>0,
    
);
$reduce = "function (obj,prev)
    {
        prev.show += obj.show; 
        prev.click += obj.click;
        prev.cost +=obj.cost;
    }
";

$re = $collection->group($keys, $initial, $reduce);</code>

Reply content:

<code>$keys = array(
    "AREA" => 1
);
$initial = array(
    "show" =>0,
    "click"=>0,
    "cost"=>0,
    
);
$reduce = "function (obj,prev)
    {
        prev.show += obj.show; 
        prev.click += obj.click;
        prev.cost +=obj.cost;
    }
";

$re = $collection->group($keys, $initial, $reduce);</code>

aggregation is what you need to use. A lot of information has been covered by searching, so I won’t go into details here. Judging from your code, it should be roughly written like this

<code>db.collection.aggregate([
    {$group: {_id: {AREA: "$AREA"}, show: {$sum: "$show"}, click: {$sum: "$click"}, cost: {$sum: "$cost"}}},
    {$limit: 1000}
]);</code>

The return is a cursor.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn