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.
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.
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 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.
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.
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
}
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With