r - rmongodb 批量插入错误:应为 mongo.bson 类对象列表 [英] r - rmongodb batch insert error: Expected a list of mongo.bson class objects

查看:42
本文介绍了r - rmongodb 批量插入错误:应为 mongo.bson 类对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

我有一份运动员名单lst_ath

lst_ath <- structure(list(`1` = structure(c("1125266", "ath_1"), .Names = c("id", 
"name")), `2` = structure(c("1125265", "ath_2"), .Names = c("id", 
"name")), `3` = structure(c("1125264", "ath_3"), .Names = c("id", 
"name")), `4` = structure(c("1125263", "ath_4"), .Names = c("id", 
"name")), `5` = structure(c("1125262", "ath_5"), .Names = c("id", 
"name")), `6` = structure(c("1125261", "ath_6"), .Names = c("id", 
"name")), `7` = structure(c("1125260", "ath_7"), .Names = c("id", 
"name")), `8` = structure(c("1125259", "ath_8"), .Names = c("id", 
"name")), `9` = structure(c("1125258", "ath_9"), .Names = c("id", 
"name")), `10` = structure(c("1125257", "ath_10", "(NZ)"), .Names = c("id", 
"name", "country"))), .Names = c("1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10"))

和一个名为athletes"的本地mongodb的连接,以及一个名为athletes"的集合

And a connection to a local mongodb called "athletes", with a collection called "athletes"

> library(rmongodb)
> mongo <- mongo.create()
> 
> #check connection
> mongo.is.connected(mongo)
[1] TRUE
> 
> #set the database
> db <- "athletes"
> mongo.get.database.collections(mongo, db)
[1] "athletes.athletes"

和一个 mongo.bson 对象 query 来自列表 lst_ath

And a mongo.bson object query from the list lst_ath

query <- mongo.bson.from.list(lst_ath)
str(query)
Class 'mongo.bson'  atomic [1:1] 0
  ..- attr(*, "mongo.bson")=<externalptr> 
>

问题

我正在尝试使用 mongo.insert.batch(mongo, db, query) 批量插入 query 对象,但出现错误:

I'm trying to batch insert the query object using mongo.insert.batch(mongo, db, query), but get an error:

> mongo.insert.batch(mongo, db, query)
Error in mongo.insert.batch(mongo, db, query) : 
  Expected a list of mongo.bson class objects

当我用 mongo.bson.from.list(lst_ath) 创建 query 时,我的 str(query) 显示 Class 'mongo.bson' atomic [1:1] 0 我无法弄清楚错误.

As I created query with mongo.bson.from.list(lst_ath), and my str(query) shows Class 'mongo.bson' atomic [1:1] 0 I can't figure out the error.

我错过了什么?

更新

以及@NicE 的解决方案,我还需要定义正在插入的集合,而不仅仅是数据库,即:

As well as @NicE 's solution, I also need to define the collection that is being inserted into, not just the database, i.e:

> #set the database
> db <- "athletes.athletes"

推荐答案

当你在你的向量列表上调用 mongo.bson.from.list 时,看起来你正在创建一个数组.由于 mongo.insert.batch 需要 query 是 'mongo.bson' 的列表,它会给你一个错误.

Looks like you are making an array when you call mongo.bson.from.list on you list of vectors. Since mongo.insert.batch needs query to be a list of 'mongo.bson' it gives you an error.

试试这个:

#make a mongo bson for each element of lst_ath
query <- lapply(lst_ath,function(x){mongo.bson.from.list(as.list(x))})


#check that query is a list of mongo.bson objects
str(query)
#List of 10
# $ 1 :Class 'mongo.bson'  atomic [1:1] 0
#  .. ..- attr(*, "mongo.bson")=<externalptr> 
# $ 2 :Class 'mongo.bson'  atomic [1:1] 0
#  .. ..- attr(*, "mongo.bson")=<externalptr> 
# $ 3 :Class 'mongo.bson'  atomic [1:1] 0
#  .. ..- attr(*, "mongo.bson")=<externalptr> 


#insert them
mongo.insert.batch(mongo, db, query)

这篇关于r - rmongodb 批量插入错误:应为 mongo.bson 类对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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