How to count objects with a specific day in mongodb and php

I have a date saved as a string similar to this tdate:"2016-11-26 2:23:11"in mongodb. I want to count all objects that have this date 2016-11-26. This is how I try to use php, but no luck.

date_authenticated has this format "Y-m-d H:i:s"so far I want to get all objects daythat should have this format"Y-m-d"

function count_auth($tdate) {
  $collection = 'mycollection';
  return $this->db->{$collection}->count(array('date_authenticated' => $tdate));
    }
+4
source share
1 answer

If you saved the date as a string, you can find records with MongoRegex, as described in PHP mongo find field starts with . In your case, it should be something like:

$startsWithRegex = '/^2016-11-26/'; 
$this->db->{$collection}->count(['date_authenticated' => array('$regex'=>new MongoRegex($startsWithRegex))]);

MongoDB, , .

+1

Source: https://habr.com/ru/post/1662273/


All Articles