Python 相当于 db.getCollectionInfos

joh*_*ase 5 python validation mongodb pymongo

我正在尝试找到一种方法来列出 mongodb 集合的所有验证规则,我想用 python 来完成

在 mongo shell 中命令是这样的:

db.getCollectionInfos()
Run Code Online (Sandbox Code Playgroud)

但是我无法在 pymongo 中识别等效命令

db.foo.collection_infos()
db.foo.getCollectionInfos()
Run Code Online (Sandbox Code Playgroud)

两者都失败了(或类似的事情):

TypeError: 'Collection' object is not callable. If you meant to call the 'get_collection_infos' method on a 'Database' object it is failing because no such method exists.
Run Code Online (Sandbox Code Playgroud)

然后我尝试将基本上随机的命令放入解释器中

db.command({"foo": "getCollectionInfos"})
Run Code Online (Sandbox Code Playgroud)

不出所料,这也不起作用。

Olu*_*ule 5

getCollectionInfosmongo shell 命令包装的命令是listCollections管理命令

管理命令可用于对数据库运行。但是,某些mongo shell 方法与管理命令共享相似的名称(例如, cloneConnectioncurrentOpdropDatabase等)。人们很容易认为其他 mongo shell 方法是管理命令,但事实并非如此。

使用 listCollections 命令,例如

# Return information for all collections in the database.
db.command('listCollections')

# Return information for collection `a`
db.command({'listCollections': 1, 'filter': {'name': 'a'}})
Run Code Online (Sandbox Code Playgroud)