- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
下面的代码是从 MongoDB 中获取数据,并在日期范围 (sum(prepayment_amount) and sum(total_prepayment_amount) group by checkin_from date)
内的图表中绘制金额和日期的总和。金额的总和位于 y 轴
中,日期位于 x 轴
中。
从这段代码中,我得到了金额和日期的总和,但问题是,
查看日期和时间2017-10-05T19:07:17Z & 2017-10-05T00:00:00Z
。在这种情况下,(sum(prepayment_amount)
和 sum(total_prepayment_amount)
日期分别绘制。
如果日期和时间是 2017-10-05T00:07:17Z & 2017-10-05T00:00:00Z
,我得到的结果是正确的。
我不知道问题出在哪里。我添加了预期的结果。任何帮助,将不胜感激。谢谢
Booking.php(模型)
protected $dates = ['bookingdate', 'checkin_from', 'reserve_to'];
DashbardController.php
public function show(Request $request)
{
if(!empty($request->cabin) && !empty($request->daterange))
{
$cabinName = $request->cabin;
$daterange = explode("-", $request->daterange);
/*$dateBegin = new \MongoDB\BSON\UTCDateTime(strtotime($daterange[0])*1000);
$dateEnd = new \MongoDB\BSON\UTCDateTime(strtotime($daterange[1])*1000);*/
$dateBegin = new \MongoDB\BSON\UTCDateTime(new DateTime($daterange[0]));
$dateEnd = new \MongoDB\BSON\UTCDateTime(new DateTime($daterange[1]));
$bookings = Booking::raw(function ($collection) use ($cabinName, $dateBegin, $dateEnd) {
return $collection->aggregate([
[
'$match' => [
'is_delete' => 0,
'cabinname' => $cabinName,
'checkin_from' => ['$gte' => $dateBegin, '$lte' => $dateEnd]
],
],
[
'$group' =>
[
'_id' => ['checkin_from' => '$checkin_from','cabinname' => '$cabinname'],
'total_prepayment_amount' => ['$sum' => '$total_prepayment_amount'],
'prepayment_amount' => ['$sum' => '$prepayment_amount'],
],
],
[
'$project' =>
[
'checkin_from' => '$_id.checkin_from',
'cabinname' => '$_id.cabinname',
'total_prepayment_amount' => 1,
'prepayment_amount' => 1
],
],
[
'$sort' =>
[
'checkin_from' => 1
],
],
]);
});
$totalPrepayAmount = [];
$prepayAmount = [];
$checkinFrom = [];
$serviceFee = [];
foreach ($bookings as $booking){
if(!empty($booking->total_prepayment_amount) && !empty($booking->prepayment_amount)) {
$checkinFrom[] = $booking->checkin_from->format('d.m.y');
$totalPrepayAmount[] = $booking->total_prepayment_amount;
$prepayAmount[] = $booking->prepayment_amount;
$serviceFee[] = round($booking->total_prepayment_amount - $booking->prepayment_amount, 2);
}
}
$chartData[] =[
'label'=> __("statisticsAdmin.totalPrepayAmount"),
'backgroundColor' => 'rgba(255, 99, 132, 0.2)',
'borderColor'=> 'rgba(255,99,132,1)',
'borderWidth'=> 1,
'data' => $totalPrepayAmount,
];
$chartData[] =[
'label'=> __("statisticsAdmin.prepayAmount"),
'backgroundColor' => 'rgba(153, 102, 255, 0.2)',
'borderColor'=> 'rgba(153, 102, 255, 1)',
'borderWidth'=> 1,
'data' => $prepayAmount,
];
$chartData[] =[
'label'=> __("statisticsAdmin.serviceFee"),
'backgroundColor' => 'rgba(79, 196, 127, 0.2)',
'borderColor'=> 'rgba(79, 196, 127, 1)',
'borderWidth'=> 1,
'data' => $serviceFee,
];
return response()->json(['chartData' => $chartData, 'chartLabel' => $checkinFrom]);
}
}
预订集合
{ "_id" : ObjectId("58046a49f8f888a80b00002a"), "cabinname" : "Matras-Haus", "checkin_from" : ISODate("2016-09-17T00:00:00Z"), "prepayment_amount" : "20", "total_prepayment_amount" : "25", "bookingdate" : ISODate("2016-09-17T06:06:01Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1), "payment_status" : "1" }
{ "_id" : ObjectId("58183678d2ae67a404431d5c"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-10-31T00:00:00Z"), "prepayment_amount" : "30", "total_prepayment_amount" : "40", "bookingdate" : ISODate("2016-11-01T06:30:16Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1), "payment_status" : "0" }
{ "_id" : ObjectId("581b31f3d2ae674d5f431d5b"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-10-31T00:00:00Z"), "prepayment_amount" : "35", "total_prepayment_amount" : "45", "bookingdate" : ISODate("2016-11-03T12:47:47Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
{ "_id" : ObjectId("5821af65d2ae67c82154efc5"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-09-05T00:00:00Z"), "prepayment_amount" : "40", "total_prepayment_amount" : "45", "bookingdate" : ISODate("2016-11-08T10:56:37Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
{ "_id" : ObjectId("582558d4d2ae679c4d8b4567"), "cabinname" : "Matras-Haus", "checkin_from" : ISODate("2016-09-17T00:00:00Z"), "prepayment_amount" : "20", "total_prepayment_amount" : "23", "bookingdate" : ISODate("2016-08-17T05:36:20Z"), "is_delete" : NumberLong(1) }
{ "_id" : ObjectId("58352c3cd2ae672341ec89e1"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-09-05T00:00:00Z"), "prepayment_amount" : "50", "total_prepayment_amount" : "55", "bookingdate" : ISODate("2016-08-05T05:42:20Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
{ "_id" : ObjectId("58352c3cd2ae672341ec887e1"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-10-05T19:07:17Z"), "prepayment_amount" : "50", "total_prepayment_amount" : "55", "bookingdate" : ISODate("2017-08-05T05:42:20Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
{ "_id" : ObjectId("58352c3cd2ae672341ec887e1"), "cabinname" : "Matras-Haus", "checkin_from" : ISODate("2017-10-05T00:00:00Z"), "prepayment_amount" : "50", "total_prepayment_amount" : "55", "bookingdate" : ISODate("2017-08-05T05:42:20Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
预期结果
prepayment_amount total_prepayment_amount checkin_from cabinname
40 48 2016-09-17 Matras-Haus
50 55 2017-10-05 Matras-Haus
65 85 2016-10-31 Kemptner Hütte
90 110 2016-09-05 Kemptner Hütte
50 55 2017-10-05 Kemptner Hütte
图片
请检查图像,在这里您可以找到问题。为了更好地理解,我还在 x 轴上列出了时间。
注意:图中数据与问题数据不匹配,因为我已将数据库与测试服务器连接。
最佳答案
在 $group
中,您将 $checkin_from
直接用作带时间的日期,因此它按日期和时间分组。
答案是使用 $dateFormat
所以我们只有日期没有时间:
'_id' => ['checkin_from' => ['$dateFormat' => ['date' => '$checkin_from', format => '%Y-%m-%d']], 'cabinname' => '$cabinname']
关于php - Mongo groupby 在 Laravel raw 方法中没有使用不同的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45568957/
我在 mongo 中有一个查询,看起来像 db.Table_Name.group({ "key": { "Camp": true }, "initial": { "Clicks": 0 }
这是一个示例查询: db.readings.find( {"_id.s": ISODate("2012-11-01T00:05:00Z") }).count() 查询在 mongo shell 中工作
我正在使用 mongo 模板查询基于状态和邮政编码的文档,这是使用以下标准和查询方法实现的: List modelClass = null; Criteria criteria = new Crite
目前,我有一个旧版本的 mongo,即 2.6 在我的系统上运行。我的网站已经投入生产,并且拥有大量客户数据。我正在计划升级到 mongo 3.2。 所以,我的问题是 mongo v3.2 的 mon
我构建了一个 MongoDB。我想通过某些分组进行聚合。我找到了 document ,这将为我做到这一点。一切正常,但确定limitations指出: 管道的输出只能包含 16 兆字节。如果你的结果s
我无法连接到 MongoDB .在 Ubuntu 中它可以工作,但我在 CentOS 工作现在。这是错误信息: MongoDB shell version: 2.4.2 connecting to:
我试图使用mongo运行最简单的mongo-express和docker-compose容器。我遇到了许多错误,这些错误将在以后解释。 我尝试了以下docker-compose配置: 1。 versi
我有一个 mongo 查询,如下所示。 db.Course.find( { $and: [{courseCallNo: {$in : [/^ssoapicall1$/i]} }, {clientId
我想知道为什么我会收到以下 php 通知: ( ! ) Notice: Mongo::__construct(): parsing servers in C:\htdocs\multishop\lib
(问题灵感来自this one) 给定一个数据集: db.mycollection.insert([ {a:1, b:2, c:3}, {a:1, b:3, c:4}, {a:0, b:1
如果我已经使用 PECL 成功安装了 mongoDB,还需要获取 debian 软件包“php5-mongo”吗?有什么不同?(这个问题应该迁移吗?) 最佳答案 区别与从 CPAN 或 debian(
Mongo 一天前还运行良好。然后今天早上我起床并尝试打开我得到这个: MongoDB shell version: 2.6.4 2015-01-06T11:10:54.142-0500 SE
我正在尝试使用 C# Mongo 驱动程序将文件上传到 Mongo Atlas。但我不明白如何连接类 MongoServerSettings。我试过这个: private static MongoSe
我有两个版本的 mongodb,2.4.3 和 2.6.0。我可以在不同的端口上启动这两个版本,但是在使用 ./mongos 运行它时出现错误: BadValue error: no args for
我需要在 不 关闭的情况下进行分片,或者在端口 27017 中重启现有的 mongo 实例。 我尝试了以下操作,(当默认端口正在运行和 mongo 实例时) mongod --shardsvr --d
在无意中升级了 mongodb 包(3.4.9 -> 3.6.1)后,有没有办法升级 mongo 数据库? 根据 mongo 文档,作为 prerequisite在升级过程中,featureCompa
我正在使用 Sails 0.12.3 和 mongo 3.2.7 这是我的 config/connections.js。 mongo: { adapter: 'sails-mongo', host
我正在使用 mongodb 构建一个基本的搜索引擎,我已经验证了基本查询在 mongo shell 中的工作。不过,我不太明白如何将其翻译成 PHP。 输入字符串中的空格表示“和”运算符和 |或管道字
我有一个用 @Document 注释的 Mongo 集合,我希望能够从字符串 (JSON) 中获取该 Java 对象,因为我们正在将这些类作为字符串插入队列。 Spring-Data-Mongo 中是
我正在使用 Linux Debian 9。我已经安装了 JDK 1.8。我使用的maven版本是3.6,springboot的版本是2.1。 mongodb版本是3.6。 下面是我试图保存在 mong
我是一名优秀的程序员,十分优秀!