Search code examples
rmongodbmongolite

Adding a column to MongoDB from R via mongolite gives persistent error


I want to add a column to a MongoDB collection via R. The collection has tabular format and is already relatively big (14000000 entries, 140 columns).

The function I am currently using is

function (collection, name, value) 
{
    mongolite::mongo(collection)$update("{}", paste0("{\"$set\":{\"", 
        name, "\": ", value, "}}"), multiple = TRUE)

    invisible(NULL)
}

It does work so far. (It takes about 5-10 Minutes, which is ok. Although, it would be nice if the speed could be improved somehow).

However, it also gives me persistently the following error that interrupts the execution of the rest of the script.

The error message reads:

Error: Failed to send "update" command with database "test": Failed to read 4 bytes: socket error or timeout

Any help on resolving this error would be appreciated. (If there are ways to improve the performance of the update itself I'd also be more than happy for any advices.)


Solution

  • the default socket timeout is 5 minutes.

    You can override the default by setting sockettimeoutms directly in your connection URI:

    mongoURI <-  paste0("mongodb://", user,":",pass, "@", mongoHost, ":", mongoPort,"/",db,"?sockettimeoutms=<something large enough in milliseconds>")
    
    mcon <- mongo(mongoCollection, url=mongoURI)
    
    mcon$update(...)