如何使用jenssegers/laravel-mongodb软件包获取文档集合中的最大日期和最小日期? [英] How to get the max and min dates in a collection of documents using jenssegers/laravel-mongodb package?

查看:261
本文介绍了如何使用jenssegers/laravel-mongodb软件包获取文档集合中的最大日期和最小日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组看起来像这样的文件:

I have a collection of documents that look like this:

{
    "_id" : ObjectId("5826182e2e94e0aefc541924"),
    "calls" : [ 
        {
            "call_date" : "2016-08-16 00:00:00.000",
            "updated_at" : ISODate("2016-11-11T19:12:46.905Z"),
            "created_at" : ISODate("2016-11-11T19:12:46.905Z"),
            "_id" : ObjectId("5826182e2e94e0aefc541925")
        }
    ],
        [ 
        {
            "call_date" : "2016-08-19 00:00:00.000",
            "updated_at" : ISODate("2016-11-11T19:12:46.905Z"),
            "created_at" : ISODate("2016-11-11T19:12:46.905Z"),
            "_id" : ObjectId("5826182e2e94f0aefc541925")
        }
    ],
        [ 
        {
            "call_date" : "2016-08-07 00:00:00.000",
            "updated_at" : ISODate("2016-11-11T19:12:46.905Z"),
            "created_at" : ISODate("2016-11-11T19:12:46.905Z"),
            "_id" : ObjectId("5826182e2j94e0aefc541925")
        }
    ]

}

我希望获得整个收藏集的最短日期和最长日期.因此,在上述情况下,我希望将2016-08-07 00:00:00.000作为最小日期,并将2016-08-19 00:00:00.000作为最大日期.我不确定是否必须使用某种聚合函数或常规查询.谢谢您的帮助

I want to be get the minimum and maximum dates across the entire collection. So in the case above, I would like to get 2016-08-07 00:00:00.000 as minimum date and 2016-08-19 00:00:00.000 as the maximum date. I am not sure if I have to use some sort of aggregate function or a regular query. Thanks for your help

推荐答案

您可以使用Laravel的收藏集:

You could use Laravel's Collection:

$collection = collect($arr['calls']);

然后,您可以使用maxmin方法,并将相关键作为参数:

And then, you can use the max and min methods with the relevant key as its argument:

$collection->max('call_date');
$collection->min('call_date');

但是我不确定这是否适用于日期字符串.如果没有,类似这样的方法应该起作用:

But I am not sure this will work for date strings. If not, something like this should work:

$value = function($item) { 
  return strtotime($item['call_date']); 
};

$collection->max($value);
$collection->min($value);

这篇关于如何使用jenssegers/laravel-mongodb软件包获取文档集合中的最大日期和最小日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆