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
Release: 2023-03-01 19:42:01
Original
1491 people have browsed it

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

The return is a cursor.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template