How to get the max and min dates in a collection of documents using jenssegers/laravel-mongodb package?

后端 未结 1 1703
余生分开走
余生分开走 2021-01-23 08:44

I have a collection of documents that look like this:

{
    \"_id\" : ObjectId(\"5826182e2e94e0aefc541924\"),
    \"calls\" : [ 
        {
            \"call_dat         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 09:38

    You could use Laravel's Collection:

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

    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);
    

    0 讨论(0)
提交回复
热议问题