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

Date in nested list doesn't recognized as a date #223

Open
lisette-bouwmeester1 opened this issue Jul 7, 2021 · 1 comment
Open

Date in nested list doesn't recognized as a date #223

lisette-bouwmeester1 opened this issue Jul 7, 2021 · 1 comment

Comments

@lisette-bouwmeester1
Copy link

lisette-bouwmeester1 commented Jul 7, 2021

Hi,

I try to add data as a nested list. This also includes a date variable. However this isn't recognized as a date, which means that I can't use $find based on date. Please find my code below:

my_list <- list(
  list(
    var1 = "string1",
    var2 = "string2",
    recorded_at =  strftime(as.POSIXlt("2021-01-01 12:00:00 CEST", "UTC") , "%Y-%m-%dT%H:%M:%S%z"),
    var3 = 70,

    map1 = list(
      left = list(
        x = 10,
        y = 100,
        z = 110
      ),      

      right = list(
        x = 20,
        y = 200,
        z = 220
      ),      

      total = list(
        x = 30,
        y = 300,
        z = 330
      )
    )
  )
)

my_df <- fromJSON(toJSON(my_list, auto_unbox = T)) # convert the nested lists to a dataframe
db$insert(my_df)

I tried to attach the type of it in the list as well:
recorded_at ='{"$date": "2021-01-01T12:00:00Z"}' but this also doesn't work.

Finally I found that it is possible to add recorded_at with $update

tm <- strftime(as.POSIXlt("2021-01-01 12:00:00 CEST", "UTC") , "%Y-%m-%dT%H:%M:%S%z")
db$update('{"var1": "string1"}', 
          paste0('{"$set": {"recorded_at": { "$date" :"', tm, '"}}}'))

This works, but I was wondering whether there is a possibility to already include date formats in the nested list. Or maybe someone has a much easier solution. Thanks!

@jonekeat
Copy link

@jeroen I also facing this issue, may I know is there any way we can specify valid mongodb data type in JSON?

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

2 participants