Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query help for date stored as string #46

Closed
SantoshSrinivas79 opened this issue Aug 3, 2016 · 5 comments
Closed

Query help for date stored as string #46

SantoshSrinivas79 opened this issue Aug 3, 2016 · 5 comments

Comments

@SantoshSrinivas79
Copy link

This code works in the MongoDB shell ..

db.eq_bhav.find({   "TIMESTAMP" : {      $gte: Math.floor(Date.parse("2015-01-01")/8.64e7)   }    })

How do I port this to mongolite? I tried the following

qry <- 'db.eq_bhav.find({"TIMESTAMP" : {"$gte": Math.floor(Date.parse("2015-01-01")/8.64e7)}})'
con$find(qry)

I get Error: Invalid JSON object

I tried adding the quotes but it still doesnt work.

Note: My dates are stored as "days" e.g. as.integer(Sys.Date())

@jeroen
Copy link
Owner

jeroen commented Aug 3, 2016

Can you try this?

query <- list(TIMESTAMP = list("$gte" =  as.POSIXct("2015-01-01")))
str <- jsonlite::toJSON(query, auto_unbox=TRUE, POSIXt = "mongo")
db.eq_bhav.find(str)

@SantoshSrinivas79
Copy link
Author

@jeroenooms I solved this using:

start_date <- as.integer(as.Date("2015-01-01"))
query <- paste('{"TIMESTAMP" : {"$gte": ',start_date,' }}', sep='')
d <- con$find(query)

But, I'm wondering if there is no way to put nodejs stuff Math.floor(Date.parse etc ... because what I was thinking is if shell is able to execute it .. there should a way to pass exactly same query strings from R and have them executed within MongoDB.
I may not understand the internals but just wanted to check.

@jeroen
Copy link
Owner

jeroen commented Feb 27, 2017

Please see the section on selecting by date in the new documentation. Note that a date in R is not the same as a Mongo datetime...

@jeroen jeroen closed this as completed Feb 27, 2017
@nimame
Copy link

nimame commented Jun 25, 2017

@jeroen: for clarification, can you confirm that this is working:

query <- list(TIMESTAMP = list("$gte" =  as.POSIXct("2015-01-01")))
str <- jsonlite::toJSON(query, auto_unbox=TRUE, POSIXt = "mongo")

Because str gives: {"TIMESTAMP":{"$gte":[{"$date":1420070400000}]}}
...and I'm think the brackets around the "$date" object are not allowed? Is there a way to remove them using jsonlite::toJSON? I'm trying to avoid concatenating strings and variables to build my queries, but stumbled over this issue.

@jeroen
Copy link
Owner

jeroen commented Jun 25, 2017

@nimame can you open a new issue for this? This might be a bug (but I don't have time to look into it immediately)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants