Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to see the pipeline definition of a MongoDB View?

I have created MongoDB view few days ago. Now I want to take a look at it again. (Query that I have written to create view). Is it possible?

I tried using collmod function db.runCommand( { collMod: 'viewName'}) but it is just returning 'Ok' as response.

I have been searching on it from hours but no luck.

like image 317
PeterB Avatar asked Jul 26 '18 14:07

PeterB


People also ask

How do I see what is in MongoDB?

If you want to check your databases list, use the command show dbs. Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it. In MongoDB default database is test.

Can we have views in MongoDB?

MongoDB provides two different view types: standard views and on-demand materialized views. Both view types return the results from an aggregation pipeline. Standard views are computed when you read the view, and are not stored to disk. On-demand materialized views are stored on and read from disk.

What is pipeline in MongoDB?

What is the Aggregation Pipeline in MongoDB? The aggregation pipeline refers to a specific flow of operations that processes, transforms, and returns results. In a pipeline, successive operations are informed by the previous result. Let's take a typical pipeline: Input -> $match -> $group -> $sort -> output.

Can we create index on view in MongoDB?

Views use the indexes of the underlying collection. As the indexes are on the underlying collection, you cannot create, drop or re-build indexes on the view directly nor get a list of indexes on the view.


1 Answers

You can use the db.getCollectionInfos() method. See the manual for the method for a detailed explanation.

For example:

> 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
    }
  }
]

The view definition is shown under the pipeline field.

Note that you can also filter by type: 'view' to display the definition of all the views in the database:

> 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
    }
  }
]
like image 190
kevinadi Avatar answered Sep 20 '22 16:09

kevinadi