jeroen / mongolite

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

How to write R NULL value in Mongo? #139

Closed gonzalezivan90 closed 6 years ago

gonzalezivan90 commented 6 years ago

Hi, I tried write a value that appears as null into mongo. I need that the field become created into the record/document in mongo. Any suggestions?

mdb <- mongo(db = ... ) mdb$insert(data.frame(one = 1, RNA = NA)) # don't write RNA field mdb$insert(data.frame(one = 1, RNULL = NULL)) # Can't create data frame mdb$insert(data.frame(na = NA, one = 1, listNull = list(NULL), asNull = as.null(1), asNullDef = as.null.default(1)) # Can't create data frame

Serenthia commented 6 years ago

Is there are reason you're not inserting using plain JSON? This inserts a value of null into Mongo:

insertObject <- jsonlite::toJSON(list("one" = 1, "RNA" = NA), auto_unbox = TRUE)
mdb <- mongo(db = ...)
mdb$insert(insertObject)

Result in Mongo is:

{ "_id" : ObjectId("5b2c1cd2b92f1f12606fa728"), "one" : 1, "RNA" : null }

gonzalezivan90 commented 6 years ago

Thanks, Serenthia. I got this error:

library(mongolite)
library(jsonlite)
> testMDB$insert(insertObjectList)
Error: is.data.frame(data) is not TRUE

other attached packages:
[1] jsonlite_1.0    mongolite_0.8.1

I have to say that my objects are data,frame-like but I can reshape them Thanks a lot

Serenthia commented 6 years ago

You need to upgrade mongolite to the latest version! Then you can use named lists or JSON strings as your input.

gonzalezivan90 commented 6 years ago

Absolutely yes! Thanks a lot. Greetings from Colombia