R:使用mongolite更新mongodb中的条目 [英] R : Updating an entry in mongodb using mongolite

查看:82
本文介绍了R:使用mongolite更新mongodb中的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mongo数据库,其中包含要传递给某些R脚本进行分析的信息.我目前正在使用mongolite包将信息从mongo传递到R.

每个mongo条目中都有一个名为checkedByR的字段,该字段是一个二进制文件,指示该条目是否已经被R脚本分析过.具体来说,我正在通过其相应的mongo ID收集mongo条目,在条目上运行脚本,为checkedByR字段分配1,然后继续.

出于完整性考虑,我正在使用以下请求查询数据库:

library(mongolite)

mongoID <- "1234abcd1234abcd1234"

m <- mongolite::mongo(url = "mongodb://localhost:27017",
                      collection = "collection",
                      db = "database")

rawData <- m$find(query = paste0('{"_id": { "$oid" : "',mongoID,'" }}'), 
                  fields = '{"_id" : 1, 
                             "checkedByR" : 1, 
                             "somethingToCheck" : 1}')

checkedByR <- 1

但是,我很难用新的checkedByR字段成功更新mongo条目.

我意识到mongolite软件包中存在update函数(请考虑: https://cran.r-project.org/web/packages/mongolite/mongolite.pdf ),但是我在收集相关示例以帮助我完成更新过程方面遇到了麻烦.

任何帮助将不胜感激.

解决方案

mongo$update()函数采用queryupdate自变量.您使用query查找要更新的数据,并使用update告诉它要更新哪个字段.

考虑此示例

library(mongolite)

## create some dummy data and insert into mongodb
df <- data.frame(id = 1:10,
                 value = letters[1:10])

mongo <- mongo(collection = "another_test", 
               db = "test", 
               url = "mongodb://localhost")

mongo$insert(df)

## the 'id' of the document I want to update
mongoID <- "575556825dabbf2aea1d7cc1"

## find some data
rawData <- mongo$find(query = paste0('{"_id": { "$oid" : "',mongoID,'" }}'), 
                      fields = '{"_id" : 1, 
                      "id" : 1, 
                      "value" : 1}')

## ...
## do whatever you want to do in R...
## ...

## use update to query on your ID, then 'set' to set the 'checkedByR' value to 1
mongo$update(query = paste0('{"_id": { "$oid" : "', mongoID, '" } }'),
             update = '{ "$set" : { "checkedByR" : 1} }')
## in my original data I didn't have a 'checkedByR' value, but it's added anyway


更新

rmongodb库不再位于CRAN上,因此下面的代码将不起作用


对于更复杂的结构&更新您可以执行的操作

library(mongolite)
library(jsonlite)
library(rmongodb)  ## used to insert a non-data.frame into mongodb

## create some dummy data and insert into mongodb
lst <- list(id = 1,
            value_doc = data.frame(id = 1:5,
                                   value = letters[1:5],
                                   stringsAsFactors = FALSE),
                        value_array = c(letters[6:10]))

## using rmongodb
mongo <- mongo.create(db = "test")
coll <- "test.another_test"

mongo.insert(mongo, 
             ns = coll, 
             b = mongo.bson.from.list(lst))

mongo.destroy(mongo)

## update document with specific ID
mongoID <- "5755f646ceeb7846c87afd90"

## using mongolite
mongo <- mongo(db = "test", 
               coll = "another_test", 
               url = "mongodb://localhost")


## to add a single value to an array
mongo$update(query = paste0('{"_id": { "$oid" : "', mongoID, '" } }'),
                         update = '{ "$addToSet" : { "value_array" :  "checkedByR"  } }')

## To add a document  to the value_array
mongo$update(query = paste0('{"_id": { "$oid" : "', mongoID, '" } }'),
                         update = '{ "$addToSet" : { "value_array" : { "checkedByR" : 1} } }')

## To add to a nested array
mongo$update(query = paste0('{"_id": { "$oid" : "', mongoID, '" } }'),
                         update = '{ "$addToSet" : { "value_doc.value" :  "checkedByR" } }')

rm(mongo); gc()


有关更多详细信息,请参见 mongodb更新文档

>

I have a mongo database with information that I am passing to some R scripts for analysis. I am currently using the mongolite package to pass the information from mongo to R.

I have a field in each mongo entry called checkedByR, which is a binary that indicates whether the entry has been analysed by the R scripts already. Specifically, I am collecting a mongo entry by its respective mongo ID, running the scripts on the entry, assigning the checkedByR field with a 1, and then moving on.

For completeness, I am querying the database with the following request:

library(mongolite)

mongoID <- "1234abcd1234abcd1234"

m <- mongolite::mongo(url = "mongodb://localhost:27017",
                      collection = "collection",
                      db = "database")

rawData <- m$find(query = paste0('{"_id": { "$oid" : "',mongoID,'" }}'), 
                  fields = '{"_id" : 1, 
                             "checkedByR" : 1, 
                             "somethingToCheck" : 1}')

checkedByR <- 1

However, I am having trouble successfully updating the mongo entry with the new checkedByR field.

I realise that an update function exists in the mongolite package (please consider : https://cran.r-project.org/web/packages/mongolite/mongolite.pdf), but I am having trouble gathering relevant examples to help me complete the updating process.

Any help would be greatly appreciated.

解决方案

the mongo$update() function takes a query and a update argument. You use the query to find the data you want to update, and the update to tell it which field to update.

Consider this example

library(mongolite)

## create some dummy data and insert into mongodb
df <- data.frame(id = 1:10,
                 value = letters[1:10])

mongo <- mongo(collection = "another_test", 
               db = "test", 
               url = "mongodb://localhost")

mongo$insert(df)

## the 'id' of the document I want to update
mongoID <- "575556825dabbf2aea1d7cc1"

## find some data
rawData <- mongo$find(query = paste0('{"_id": { "$oid" : "',mongoID,'" }}'), 
                      fields = '{"_id" : 1, 
                      "id" : 1, 
                      "value" : 1}')

## ...
## do whatever you want to do in R...
## ...

## use update to query on your ID, then 'set' to set the 'checkedByR' value to 1
mongo$update(query = paste0('{"_id": { "$oid" : "', mongoID, '" } }'),
             update = '{ "$set" : { "checkedByR" : 1} }')
## in my original data I didn't have a 'checkedByR' value, but it's added anyway


Update

the rmongodb library is no longer on CRAN, so the below code won't work


And for more complex structures & updates you can do things like

library(mongolite)
library(jsonlite)
library(rmongodb)  ## used to insert a non-data.frame into mongodb

## create some dummy data and insert into mongodb
lst <- list(id = 1,
            value_doc = data.frame(id = 1:5,
                                   value = letters[1:5],
                                   stringsAsFactors = FALSE),
                        value_array = c(letters[6:10]))

## using rmongodb
mongo <- mongo.create(db = "test")
coll <- "test.another_test"

mongo.insert(mongo, 
             ns = coll, 
             b = mongo.bson.from.list(lst))

mongo.destroy(mongo)

## update document with specific ID
mongoID <- "5755f646ceeb7846c87afd90"

## using mongolite
mongo <- mongo(db = "test", 
               coll = "another_test", 
               url = "mongodb://localhost")


## to add a single value to an array
mongo$update(query = paste0('{"_id": { "$oid" : "', mongoID, '" } }'),
                         update = '{ "$addToSet" : { "value_array" :  "checkedByR"  } }')

## To add a document  to the value_array
mongo$update(query = paste0('{"_id": { "$oid" : "', mongoID, '" } }'),
                         update = '{ "$addToSet" : { "value_array" : { "checkedByR" : 1} } }')

## To add to a nested array
mongo$update(query = paste0('{"_id": { "$oid" : "', mongoID, '" } }'),
                         update = '{ "$addToSet" : { "value_doc.value" :  "checkedByR" } }')

rm(mongo); gc()


see mongodb update documemtation for further details

这篇关于R:使用mongolite更新mongodb中的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆