jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
284 stars 64 forks source link

inserting data.frame with POSIXt column containing NA values fails when setting na="null" #176

Closed svdwoude closed 4 years ago

svdwoude commented 5 years ago

When inserting a data.frame with a POSIXt column containing NAs mongolite throws an Invalid read of null in state IN_BSON_TYPE error when setting na = "null"

library(mongolite)
mongo <- mongo()
df <- data.frame(date = c(Sys.time(),NA))
mongo$insert(df, auto_unbox = TRUE, na = "null") 
#> Error: Invalid read of null in state IN_BSON_TYPE

This does work without the na = "null" option but returns unexpected json format. For all other types, the values are dropped, but here we get {} back

library(mongolite)
mongo <- mongo()
df <- data.frame(date = c(Sys.time(),NA))
mongo$insert(df, auto_unbox = TRUE) 

in mongo this is saved as:

{
    "_id" : ObjectId("5cb8283c9bee475a6762dfc2"),
    "date" : ISODate("2019-04-21T02:33:13.319Z")
},
{
    "_id" : ObjectId("5cb8283c9bee475a6762dfc3"),
    "date" : {}
}