Mongolite不能将带有列表列的数据框正确插入Mongo DB

时间:2018-10-15 19:13:40

标签: r json mongodb mongolite

我将给出一个简短的,可复制的问题示例,其中涉及将R中的数据插入到mongo数据库中。正如您将看到的那样,这具有挑战性,因为我有一个嵌套的数据列。解决这个问题对我的数据库至关重要,我认为这也是其他人也可能遇到的问题。

我的数据:

my.data <- structure(list(`_id` = c(10138L, 9466L, 9390L), firstName = c("Alex", "Quincy", "Steven"), lastName = c("Abrines", "Acy", "Adams"), 
    birthCity = c("Palma de Mallorca", "Tyler, TX", "Rotorua"
    ), birthCountry = c("Spain", "USA", "New Zealand")), row.names = c(NA, 
3L), class = "data.frame")

my.data
> nba_players
    _id firstName lastName         birthCity birthCountry
1 10138      Alex  Abrines Palma de Mallorca        Spain
2  9466    Quincy      Acy         Tyler, TX          USA
3  9390    Steven    Adams           Rotorua  New Zealand

inner.df <- structure(list(jerseyNumber = 40L, weight = 240L, age = 21L), class = "data.frame", row.names = 485L)

num.vector <- c(1,3,5,7)

我的目标是双重的:

  • inner.df上向num.vector添加第四列
  • inner.df作为第六列添加到my.data中的每一行

...这是我用来执行此操作的代码:

# add a list of the numbers to inner df
inner.df$shotIDs = list(num.vector)  

# create allmonths column (name of the row where inner.df's will be placed)  
my.data <- my.data %>%
  dplyr::mutate(allmonths = NA)

# convert allmonths into a column of class == list
my.data$allmonths[1] = list(placeholder = NA)

# For EACH row in my main my.data dataframe, add the inner.df to the allmonths column/key
for(i in 1:nrow(my.data)) {
  my.data$allmonths[[i]] <- inner.df
}

# Write this to my mongo db
con <- mongolite::mongo(collection = 'mycoll', db = 'mydb', url = "myurl")
con$insert(my.data) # this is not a good way to update a db

这是我的结果(来自Robo 3T):

enter image description here ...

我对此非常满意,但是由于某种原因,allmonths是一个长度为1的数组,而不是它自己的对象。如果allmonths是一个具有4个字段的对象,且其值与标记为[0]的对象的值完全相同,则效果会更好。

有人在我这里尝试的过程中发现问题吗?我确定这是在R中使用嵌套对象时其他人可能遇到的问题!任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:1)

要获取对象{ },您的allmonths必须是类型data.frame的列,而不是list

以你的榜样

library(dplyr)

my.data <- structure(list(`_id` = c(10138L, 9466L, 9390L), firstName = c("Alex", "Quincy", "Steven"), lastName = c("Abrines", "Acy", "Adams"), 
                          birthCity = c("Palma de Mallorca", "Tyler, TX", "Rotorua"
                          ), birthCountry = c("Spain", "USA", "New Zealand")), row.names = c(NA, 
                                                                                             3L), class = "data.frame")


my.data

inner.df <- structure(list(jerseyNumber = 40L, weight = 240L, age = 21L), class = "data.frame", row.names = 485L)

num.vector <- c(1,3,5,7)

# add a list of the numbers to inner df
inner.df$shotIDs = list(num.vector)  

如果您现在将inner.df附加为一列(因为您需要3行才能与my.data匹配,所以必须重复)

my.data$allmonths <- inner.df[rep(1,3), ]

然后查看它产生的JSON,您看到得到了allmonths: { }对象

substr( jsonlite::toJSON( my.data ), 1, 196 )
# [{"_id":10138,"firstName":"Alex","lastName":"Abrines","birthCity":"Palma de Mallorca","birthCountry":"Spain",
# "allmonths":{"jerseyNumber":40,"weight":240,"age":21,"shotIDs":[1,3,5,7],"_row":"485"}
# } 

在旁边

构造您想要的JSON通常很有帮助,然后调用fromJSON来查看您应针对的R结构

js <- '
[{"_id":10138,"firstName":"Alex","lastName":"Abrines","birthCity":"Palma de Mallorca","birthCountry":"Spain","allmonths":{"jerseyNumber":40,"weight":240,"age":21,"shotIDs":[1,3,5,7],"_row":"485"}},{"_id":9466,"firstName":"Quincy","lastName":"Acy","birthCity":"Tyler, TX","birthCountry":"USA","allmonths":{"jerseyNumber":40,"weight":240,"age":21,"shotIDs":[1,3,5,7],"_row":"485.1"}},{"_id":9390,"firstName":"Steven","lastName":"Adams","birthCity":"Rotorua","birthCountry":"New Zealand","allmonths":{"jerseyNumber":40,"weight":240,"age":21,"shotIDs":[1,3,5,7],"_row":"485.2"}}] 
'
str( jsonlite::fromJSON( js ) )

# 'data.frame': 3 obs. of  6 variables:
#   $ _id         : int  10138 9466 9390
#   $ firstName   : chr  "Alex" "Quincy" "Steven"
#   $ lastName    : chr  "Abrines" "Acy" "Adams"
#   $ birthCity   : chr  "Palma de Mallorca" "Tyler, TX" "Rotorua"
#   $ birthCountry: chr  "Spain" "USA" "New Zealand"
#   $ allmonths   :'data.frame':    3 obs. of  4 variables:
#   ..$ jerseyNumber: int  40 40 40
#   ..$ weight      : int  240 240 240
#   ..$ age         : int  21 21 21
#   ..$ shotIDs     :List of 3
#   .. ..$ : int  1 3 5 7
#   .. ..$ : int  1 3 5 7
#   .. ..$ : int  1 3 5 7