Open koheiw opened 4 years ago
You are correct, this is a bug. The issue is jsonlite not honoring auto_unbox
icw/ POSIXt="mongo"
:
mylist <- list(x = "foo", today = Sys.time())
jsonlite::toJSON(mylist, POSIXt = 'mongo')
# {"x":["foo"],"today":[{"$date":1586800884247}]}
jsonlite::toJSON(mylist, POSIXt = 'mongo', auto_unbox = TRUE)
# {"x":"foo","today":[{"$date":1586800884247}]}
I'll try to fix it. One workaround is to explicitly unbox()
the element:
mylist <- list(x = "foo", today = jsonlite::unbox(Sys.time()))
jsonlite::toJSON(mylist, POSIXt = 'mongo')
# {"x":["foo"],"today":{"$date":1586800912819}}
Or alternatively you can insert the data as a data frame, or as literal json.
Thanks @jeroen for quick response. I will use data.frame
for workaround in my teaching!
@jeroen Thanks for the great package. I am using your package to introduce my students to MongoDB. I am filing this issue because I was not sure if this is the expect behavior or not.
The question is should values save by
insert(x)
differ betweendata.frame
andlist
? As you can see in the screen shot of MongoDB Compass,created_at
isdate
only whenx
is adata.frame
. Whenx
is alist
it becomes a BSON array. The code below is taken from the package website and modified to highlight the issue.I though I can change how
insert()
behaves for alist
ifauto_unbox = TRUE
, but I did not see any difference.