MongoDB - quick view of the "view"

Quick overview about Mongodb view

A view is merely a saved aggregation pipeline that's called whenever you reference the name of the view as though you were querying a collection. You cannot modify the view

Here are a few simply facts about MongoDB View:

1. It's read only
2. View must be created in the same db as the based collection
3. It's a good alternative to mask sensitive data
4. Cannot be renamed , do drop and create instead
5. It doesn't materialized, meaning view does not store anything but definition


To create the view with real example:

db.getSiblingDB("personnel-core").createView("nameView",name,  [{
                                        "$project" : {
                                                "fullName" : {
                                                        "$concat" : [
                                                                "$firstName",
                                                                " ",
                                                                "$lastName"
                                                        ]
                                                },
                                                "firstName" : 1,
                                                "middleName" : 1,
                                                "lastName" : 1,
                                                "nameTypeId" : 1,
                                                "effectiveUntil" : 1,
                                                "employeeId" : 1
                                        }
                                }
                        ], )


To get the definition of the view

db.getSiblingDB("personnel-core").getCollectionInfos({name:"nameView"})
[
        {
                "name" : "nameView",
                "type" : "view",
                "options" : {
                        "viewOn" : "name",
                        "pipeline" : [
                                {
                                        "$project" : {
                                                "fullName" : {
                                                        "$concat" : [
                                                                "$firstName",
                                                                " ",
                                                                "$lastName"
                                                        ]
                                                },
                                                "firstName" : 1,
                                                "middleName" : 1,
                                                "lastName" : 1,
                                                "nameTypeId" : 1,
                                                "effectiveUntil" : 1,
                                                "employeeId" : 1
                                        }
                                }
                        ]
                },
                "info" : {
                        "readOnly" : true
                }
        }
]


To drop the view with real example:

db.getSiblingDB("personnel-core").nameView.drop()













Comments

Popular posts from this blog

MongoDB tip: 4 ways to modify replica set configuration

MongoDB Quick Note: BI Connector Issue

MongoDB Tips: Kill long running processes