+
95
-

php如何对mongodb最近几日数据分组统计?

php如何对mongodb最近几日数据分组统计?例如统计近7日每天新增用户量。


网友回复

+
15
-

首先插入mongodb数据库中的字段为int时间戳格式,然后通过project转换成日期格式,示例代码如下:

<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;

$bulk->insert(['x' => 2, 'name' => 'bfw', 'scores' => 123, 'atime' => time()-1000000]);
$bulk->insert(['x' => 2, 'name' => 'google', 'scores' => 10, 'atime' => time()-200000]);
$bulk->insert(['x' => 2, 'name' => 'baidu', 'scores' => 11, 'atime' => time()-1231123]);
$bulk->insert(['x' => 2, 'name' => 'bfw', 'scores' => 200, 'atime' => time()]);
$bulk->insert(['x' => 2, 'name' => 'bfw', 'scores' => 123, 'atime' => time()]);
$bulk->insert(['x' => 2, 'name' => 'google', 'scores' => 10, 'atime' => time()]);
$bulk->insert(['x' => 2, 'name' => 'baidu', 'scores' => 11, 'atime' =>...

点击查看剩余70%

我知道答案,我要回答