有可能看到MongoDB视图的管道定义吗



我几天前创建了MongoDB视图。现在我想再看一遍。(我为创建视图而编写的查询(。有可能吗?

我尝试使用colmod函数db.runCommand( { collMod: 'viewName'}),但它只是返回"Ok"作为响应。

我已经找了好几个小时了,但没有找到。

您可以使用db.getCollectionInfos()方法。有关详细说明,请参阅手册中的方法。

例如:

> db.createView('testview', 'test', {$project: {a:1, b:1}})
> db.getCollectionInfos({name:'testview'})
[
{
"name": "testview",
"type": "view",
"options": {
"viewOn": "test",
"pipeline": [
{
"$project": {
"a": 1,
"b": 1
}
}
]
},
"info": {
"readOnly": true
}
}
]

视图定义显示在pipeline字段下。

注意,您还可以通过type: 'view'进行筛选,以显示数据库中所有视图的定义:

> db.getCollectionInfos({type:'view'})
[
{
"name": "testview",
"type": "view",
"options": {
"viewOn": "test",
"pipeline": [
{
"$project": {
"a": 1,
"b": 1
}
}
]
},
"info": {
"readOnly": true
}
},
{
"name": "testview2",
"type": "view",
"options": {
"viewOn": "test",
"pipeline": [
{
"$group": {
"_id": null,
"count": {
"$sum": 1
}
}
}
]
},
"info": {
"readOnly": true
}
}
]

最新更新