无法使用mongolite查看来自mongodb远程服务器的所有集合 [英] Unable to see all collections from a mongodb remote server using mongolite

查看:170
本文介绍了无法使用mongolite查看来自mongodb远程服务器的所有集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个小问题.我能够连接到远程mongodb服务器.我正在使用mongolite连接数据库.我的数据库是mobileapps.我也不知道在集合"中要指定什么.我知道我可以指定任何集合.

This may be a trivial problem. Im able to connect to remote mongodb server. Im using mongolite for connecting to the db. My DB is mobileapps. I also dont know what to specify in 'collection'. i know i can specify any collection.

library(mongolite)
con=mongo(collection = "test", url = "mongodb://user:password@ds035965.mongolab.com:35965/mobileapps")

虽然正在连接,但不显示任何数据.我也不明白为什么它会在str(con)上显示出来:为什么在那里出现jeroen.

Though it is connecting, but doesnt show any data. Also i dont understand why does it show this for str(con): why is jeroen there.

Classes 'mongo', 'jeroen', 'environment' <environment: 0x0000000014a8ec00>

正在连接,但我看不到所有收藏.我如何查看数据库中的所有集合.

It is connecting but i am unable to see all the collections. how do i see all the collections in the db.

另外,如何在不查询列名称,类型等情况下,对集合进行一些基本统计.我只能使用con $ count()对数据库中的行进行计数. 类似于cmd提示符下的db.getCollectionNames().

Also how do some basic statistics about the collection without querying like column names, types. i could only use con$count() to count the rows in the db. something similar to db.getCollectionNames() from cmd prompt.

更新1

感谢我的理解,我在从mongolite连接时必须指定一个特定的集合.但是我如何使用Rmongodb进行连接仍然是一个问题.

Thanks i understand that i have to specify a particular collection while connecting from mongolite. But how do i connect using Rmongodb is still an issue.

mongo.create(host = "ds035965.mongolab.com", name = "MobileApp1", username = "user", password = "password ", db = "mobileapps")

这给了我一个错误:

Unable to connect to replset
Authentication failed.

更新2

当我使用 rmongodb 连接到本地主机时,会出现此错误.

when i connect to my local host using rmongodb i get this error.

Error in as.environment(pos) : invalid 'pos' argument

即使我无法看到数据库和其中的集合,我仍然会收到此错误.对发生的事情有任何想法.

Even though im able to see the db and the collection within i still get this error. Any thoughts on whats happening.

推荐答案

mongolite 要求您连接到数据库的特定集合.初始化mongo对象时,您会看到dbcollection参数默认为"test"

mongolite requires you to connect to a specific collection of a database. When you initialise the mongo object, you see that the db and collection arguments default to "test"

?mongolite::mongo

mongo(collection ="test",db ="test",url ="mongodb://localhost",详细= TRUE)

mongo(collection = "test", db = "test", url = "mongodb://localhost", verbose = TRUE)

因此,当您使用

初始化con

con=mongo(collection = "test", url = "mongodb://user:password@ds035965.mongolab.com:35965/mobileapps")

即使您未指定将其连接到特定数据库,它也将连接到 db = test ,因为这是默认设置.因此,如果您没有名为 test 的数据库,则该数据库将不包含任何数据.这就解释了为什么看不到任何数据.

Even though you haven't specified it to connect to a particular database, it is connecting to db = test, because that is the default. And, therefore, if you don't have a database called test, it won't contain any data. Which explains why you're not seeing any data.

根据我的理解(很高兴得到纠正),mongolite不可能查看数据库中的所有集合,因为您必须专门连接到其中一个.

It's my understanding (and I'm happy to be corrected) that with mongolite it's not possible to look at all the collections in a database, as you have to specifically connect to one.

如果要查看集合,可以尝试rmongodb软件包

If you want to see the collections, you can try the rmongodb package

library(rmongodb)
mongo <- mongo.create()
mongo.get.databases(mongo)
## returns databases

但是,要查看数据库中的所有集合,您不能使用该功能

However, to see all the collections in the database you can't use the function

mongo.get.database.collections(mongo, "test")

,因为它返回一个空字符串.这是已知问题

as it return an empty string. This is a known issue

一种解决方法是使用

## return all the collections in the database 'test'
mongo.command(mongo = mongo, db = "test", command = list(listCollections=1))

mongo.destroy(mongo)

这篇关于无法使用mongolite查看来自mongodb远程服务器的所有集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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