简体   繁体   English

如何在R中列出mongodb集合

[英]How to list mongodb collections in R

I have been trying to list collections within a mongo database in R. I have realized that this feature is still in the to do list in the mongolite package ( https://github.com/jeroen/mongolite/issues/86 ). 我一直在尝试在R中的mongo数据库中列出集合。我意识到,此功能仍在mongolite软件包( https://github.com/jeroen/mongolite/issues/86 )的待办事项列表中。 There seemed to be a package, rmongodb , which did the trick ( Unable to see all collections from a mongodb remote server using mongolite ). 似乎有一个软件包rmongodb可以达到rmongodb无法使用mongolite查看mongodb远程服务器上的所有集合 )。 However, it is no longer a part of CRAN. 但是,它不再是CRAN的一部分。

Can anyone, please, suggest a way to list all collection in a database? 任何人都可以建议一种列出数据库中所有集合的方法吗?

The mongodb is remote, so I guess using the mongoshell with combination with system() is not an option. mongodb是远程的,因此我猜不能将mongoshell与system()结合使用。 At least not a straightforward one. 至少不是一个简单的方法。

Thanks 谢谢

The solution I came up with is the following: 我想出的解决方案如下:

ListMongoCollections <- function(db, mongoConString) {

  result <- system(glue::glue(
    "
    mongo --host <<mongoConString>> --eval \"
      db.getMongo().getDBNames().forEach(
        function(v, i) {if (v.valueOf() === '<<db>>') {
          print(db.getSiblingDB(v).getCollectionNames().join('%%%'))
        }}
      )
    \"
    ",
    .open = "<<",
    .close = ">>"
    ),
    intern = T
  )

  collections <- result %>% stringr::str_detect("%%%")

  result <- result[collections] %>% 
    stringr::str_split(pattern = "%%%", simplify = T) %>% 
    as.character()

  result

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM