jeroen / mongolite

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

Adding NaN and Infinity values to database #91

Closed LauraPalas closed 6 years ago

LauraPalas commented 7 years ago

Hi @jeroen ,

I'm trying to update a field in my database with a NaN value with this code: mongo_stats$update('{"user": {"$oid" : "58b011c1d61ac00116e88888"}}', '{"$set": {"hola": NaN}}')

However, R does not recognize NaN although Mongo does because running this in the command line works just fine: db.userstats.update({user: ObjectId("58b011c1d61ac00116e88888")},{"$set":{"hola":NaN}})

I have also tried it with Infinity (and -Infinity) and it does not work either with mongolite in R, but with mongo it does again.

Please, I would be very grateful if you could help me with this issue because I need to upload NaN and Infinity values in my database and I don't know how.

Thank you very much, Laura

jeroen commented 6 years ago

Use mongo's extended json syntax, for example:

m <- mongo()
json <- c(
  '{"x": 123}',
  '{"x":  { "$numberDouble": "NaN" }}',
  '{"x":  { "$numberDouble": "Infinity" }}'
)

m$insert(json)
df <- m$find()
df$x

Gives a numeric vector:

[1] 123 NaN Inf