1

I have documents stored in MongoDB with each documents containing a "created_at" that looks like this:

"created_at" : "Wed Jan 24 15:25:20 +0000 2018"

I couldn't do a range query with a format like this. Should I update all of the documents and convert them into readable dates? Or is there a way in PHP where I should just convert the dates inside the PHP without updating/harming the documents inside MongoDB. The latter seems to be more sensible since I am constantly feeding my database with streams of tweets everyday.

codex
  • 432
  • 2
  • 7
  • 18

1 Answers1

0

You should reconsider the format you are using to store the created_at timestamp. I would recommend to convert this data to ISODate (js)/MongoDB\BSON\UTCDateTime (php).

Using this format, you can do range queries in your MongoDB without heavy lifting on your php side.

Converting the data includes the following steps:

  • One time data conversion (you update the already existing data in youor MongoDB)
  • Changing the data inflow to use the new format for all newly added data
  • Changing the view components to handle the new format.

Some relevant links:

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
  • ah, so I really have to perform an update in my documents inside the database and convert the dates into either of these `ISODate` or `MongoDB\BSON\UTCDateTime`? – codex Jan 25 '18 at 13:06
  • 1
    MongoDB does all the heavy lifting with dates for you if you use `MongoDB\BSON\UTCDateTime`. So yes, that is what I would recommend. – rollstuhlfahrer Jan 25 '18 at 13:22
  • Wouldn't that be expensive considering I will be feeding the database constantly everyday? That'd be like hardcoded in my PHP file where it will update all of the documents in my database every time the user refreshes the webpage? – codex Jan 25 '18 at 13:29
  • 1
    I've edited my answer. Basically you update the data persistently in your MongoDB and thus don't have to do the conversion every time you want run run a range query. – rollstuhlfahrer Jan 25 '18 at 13:38