gpt4 book ai didi

python - 无法使用 mongodb 协议(protocol)支持列出 azure document db 中的集合

转载 作者:可可西里 更新时间:2023-11-01 10:02:47 25 4
gpt4 key购买 nike

我目前正在使用 Python3 (pymongo) 连接到支持 Mongo 协议(protocol)的 Azure 文档数据库。

# reference to connection string
self.connection_string = "mongodb://<user>:<pw>@<location>:<port>/<database>?ssl=true"

# creates the connection (this is working)
self.mongo_client = MongoClient( self.connection_string )

# show databases and there collections
print(self.mongo_client.database_names())
for db_name in self.mongo_client.database_names():
print(db_name,">",self.mongo_client[db_name].collection_names())

运行上面的代码片段会列出数据库,但不会列出集合。在本地 mongo 数据库上运行它可以按预期工作。

我最初尝试对数据库中的已知集合运行查询,但是,我什至似乎无法做到这一点。我已连接 MongoChef 并且正在按预期工作(能够运行查询)。

你知道我可能做错了什么吗?

最佳答案

@Greener,该问题似乎是由 pymongo 引起的,而不是由 DocumentDB with MongoDB 协议(protocol) 引起的。

我尝试使用第三方工具Robomongo成功连接DocumentDB与MongoDB协议(protocol),并且我可以看到如下的集合。

enter image description here

作为引用,这里是通过数据库级​​别命令 listCollections 在 PyMongo 中列出集合的解决方法,请参见下文。

>>> mongo_client.command('listCollections')
{'ok': 1, '_t': 'ListCollectionsResponse', 'cursor': {'firstBatch': [{'options': {}, 'name': 'testCollection'}], 'ns': 'testdb.$cmd.listCollections', 'id': 0}}

希望有帮助。

关于python - 无法使用 mongodb 协议(protocol)支持列出 azure document db 中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40525520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com