Geiser
Geiser

Reputation: 1089

R - MongoDB: Best way to store Dates inserted from R

I would like to know what's the best way to store date and time objects in MongoDB. I don't mind what type, if POSIXct or POSIXlt or Date object.

I ask this question because I want an easy way to then query from R a range of dates, so that R only processes and plots those values.

Thank you

EDIT:

There are methods included in JavaScript like here, or by MongoDB itself, here, but I would like to find an easy way to insert and then query it, all in R language, like "show me all the values between this range of dates".

Maybe the problem itself is how to organize it better when querying. Currently I have a running app that stores the time objects in the way of: (The comment is an example of the output)

as.character(strptime(Sys.time(), "%F %X"))
## 2015-01-01 12:00:00

But when I have to plot it, I get all the data in the collection. Then I process the dates inside a ggplot(), showing only the required dates.

This is very time-consuming and inefficient. I tried to perform a $regex to the MongoDB query, but that would make things so difficult.

Upvotes: 1

Views: 633

Answers (1)

Sagaponack FX
Sagaponack FX

Reputation: 56

I think this will help:

http://blog.mongodb.org/post/65517193370/schema-design-for-time-series-data-in-mongodb

Storing the data properly will speed up your query.

Also, what library are you using? RmongoDB is ideal for speed and flexibility.

Then, you'll want to do a BSON query from within R on the data you want instead of requesting everything.

Upvotes: 1

Related Questions