How to Update A boolean value in mongo using mongoite in R

592 Views Asked by At

I am unable to update a bool value in mongo using mongolite. I have a bool value (FALSE) in r and I am trying to update a field in mongo which is currently having value true(mongo bool). But After making update command using mongolite, stored bool value true in mongo changed to string value FALSE(R bool type).

1

There are 1 best solutions below

0
On

mongolite doesn't automatically update r booleans as mongo booleans.

this throws an Error: Invalid JSON object for me:

m$update('{"name":"foo", "$set":{"boolean":FALSE}}')

this inserts FALSE as string:

m$update('{"name":"foo", "$set":{"boolean":"FALSE"}}')

this inserts false as boolean:

m$update('{"name":"foo", "$set":{"boolean":false}}')

if you're making the update query programmatically you can do something like this:

```

my_boolean <- FALSE
my_updateQuery <- paste0('{"$set":{"boolean":',tolower(paste0(my_boolean)),'}}')
m$update('{"name":"foo", my_updateQuery)

```