0

In mongodb created_on values is like YYYY-mm-dd H:i:s e.g 2017-08-03 11:51:05. but while searching outputs i have to pass only date YYYY-mm-dd but it is not working with Yii2 mongodb activerecord but it is working from terminal.

Mongodb activerecord :

$output  = DeviceOutput::find()
                ->where(["created_on"=>"/^$date/"])
                ->one();

Terminal Command :

db.device_output.find({"created_on":/^2017-08-03/})

How to do it in PHP ?

Yatin Mistry
  • 1,246
  • 2
  • 13
  • 35
  • It's "much better" to actually convert the "strings" to a BSON Date and then [use a "range" to select between the dates](https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb) ( a single day is in fact a "range" ). Stored as a BSON Date, this in fact takes **4-bytes** ( length of the long integer value for the timestamp ) as compared to the **24-bytes** to store the "string". Not only does storage take more space, but that also equates to time to load and process, and it's a significant difference, especially over a large amount of data. – Neil Lunn Aug 08 '17 at 09:15
  • That said, your query fails because you are constructing a "string" rather than a "regular expression" like what is actually being done in the shell. Either create a valid regular expression or use the `$regex` BSON expression. But **ideally** ( and realistically ) you really should fix the dates from being strings in the first place. – Neil Lunn Aug 08 '17 at 09:17
  • @NeilLunn can you give me example how should i do in php mongodb / yii2 mongodb activerecord? – Yatin Mistry Aug 09 '17 at 06:34

0 Answers0