Search code examples
mongodbviewmongodb-.net-driver

How do I find out which pipelines have been used for creating a view?


What is the shell command for finding out which pipelines have been used for creating a certain view? MongoDB version is 3.6.4

Also, is there a way of getting the pipelines with the .net driver, version 2.6.1?


Solution

  • You can use db.getCollectionInfos(), and optionally specify a "filter" condition as a query.

    For example:

    // Insert Collection
    db.test.insert({ "a": 1 })
    
    // Create a view
    db.createView("testView", "test", [{ "$match": { } }]);
    
    
    // Get the information
    db.getCollectionInfos({ "name": "testView" })
    [
            {
                    "name" : "testView",
                    "type" : "view",
                    "options" : {
                            "viewOn" : "test",
                            "pipeline" : [
                                    {
                                            "$match" : {
    
                                            }
                                    }
                            ]
                    },
                    "info" : {
                            "readOnly" : true
                    }
            }
    ]
    

    Note that "name" matches a specific collection, and you can optionally even filter for "view" on the "type" field to get all views. The pipeline is clearly displayed in the returned output.

    Note also this "shell method" just wraps the listCollections system command. Most drivers have some variant of this method on the "database" object or they can otherwise just invoke the command with options.