I'm using the following query and trying to write the results to a .csv file:
.libPaths("G:/R/R-3.6.1/library")
library(mongolite)
library(data.table)
library(dplyr)
###Path to list of search terms
#------------------------------------------------------------------------------------------------------------#
#Input the words you want to search in a text file, and call the text file below in place of ".txt"
#-------------------------------------------------------------------------------------------------------------#
searchStrings = readLines("//int/elm/Work/Text Analytics/Opportunities/New Folder/terms.txt")
searchCounts = data.table(searchString = searchStrings, count = 0)
db <- mongo(collection = "2020Emails", db = "textAnalytics", verbose = TRUE,
            url = "mongodb://user:m0ng0b0ng0@interactionsprojection:7999/textAnalytics")
firstQuery = TRUE
#Update this JSON if if you want different fields returned
fieldsjson = '{"DocEid" : true,
"RequestType" : true,
"FromEmailAddress" : true,
"ReceiptTime" : true,
"RawBodyText" : true,
"CMF" : true,
"ReplyTo" : true,
"InboundMode" : true,
"PartNumbers" : true,
"_id": false}'
for (term in searchStrings) {
  queryString = paste0('{"$text" : { "$search" : "\\"',term,'\\"" }}')
  if (firstQuery == TRUE) {
    results = NULL
    results = data.table(db$find(query = queryString, fields = fieldsjson))
    if (nrow(results) > 0) {
      results[, searchString := term]
      searchCounts[searchString == term, count := nrow(results)]
      firstQuery = FALSE
    }
  } else {
    tempdt = data.table(db$find(query = queryString, fields = fieldsjson))
    if (nrow(tempdt) > 0) {
      tempdt[, searchString := term]
      results = rbind(results,tempdt, fill= TRUE)
      searchCounts[searchString == term, count := nrow(tempdt)]
    }
  } 
}
View(results)
#------------------------------------------------------------------------------#
# Specify the location and new file name where the results will be stored below
#------------------------------------------------------------------------------#
fwrite(results, "C:/Results/terms.csv")
I am getting the following error when trying to write this to a .csv. file.
Error in fwrite(results, "C:/Results/terms.csv") : Row 1 of list column is type 'list' - not yet implemented. fwrite() can write list columns containing items which are atomic vectors of type logical, integer, integer64, double, complex and character.
