如何在 R 中列出 mongodb 个集合

How to list mongodb collections in R

我一直在尝试在 R 中的 mongo 数据库中列出集合。 我已经意识到此功能仍在 mongolite 包 (https://github.com/jeroen/mongolite/issues/86). There seemed to be a package, rmongodb, which did the trick () 的待办事项列表中。但是,它不再是 CRAN 的一部分。

任何人都可以建议一种在数据库中列出所有集合的方法吗?

mongodb 是远程的,所以我猜想将 mongoshell 与 system() 结合使用不是一种选择。至少不是直截了当的。

谢谢

我想到的解决方案如下:

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

}