Navicat Blog

Sep 18, 2018 by Robert Gravelle

In relational databases, a view is a searchable data subset that is defined by a query. Views are sometimes referred to as "virtual tables" because they don't store data, but can be queried just like tables. MongoDB recently introduced views in version 3.4. In today's blog, we'll learn how to create a view in MongoDB using Navicat for MongoDB GUI administration and development tool.

Opening the View Object List

There are 2 ways to open the view object list from the Navicat main window:

  • Click the View command button from the main toolbar.
  • Select the Views object in the Database Objects tree.

The Navicat View Designer

The View Designer is a specialized Navicat for MongoDB tool for creating and editing your views. It's accessible via the New View button from the Objects Tab toolbar:

You can also right-click (Ctrl+Click on macOS) the the Views object in the Database Objects tree and select New View from the pop-up menu:

TIP: You can create a view shortcut by right-clicking (Ctrl+Click on macOS) a view in the Objects tab and select Create Open View Shortcut from the pop-up menu. This option is used to provide a convenient way for you to open your view directly in the Navicat main window.

Creating a View

When you create a view in MongoDB, the engine runs an aggregation. Hence, creating a view requires that we specify a collection or an existing view.

We'll create a view that only shows actors' full names.

On the General tab, choose the actor collection from the Collection/View dropdown list.

Now click on the Pipeline tab. It contains a dropdown list of Operators along with an Expression text field.

MongoDB features a number of Operators for constructing expressions for use in the aggregation pipeline stages that shape your view. Operator expressions are similar to functions that take arguments. In general, these expressions take an array of arguments and have the following form:

{ <operator>: [ <argument1>, <argument2> ... ] }

The Operator we need to select from the list is $project. It passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input document or newly computed fields.

Here is an Expression that suppresses the _id field and concatenates the first_name and last_name fields from the actor collection.

{ _id: 0, full_name : { $concat: ["$first_name", ", ", "$last_name"] } }

You can view the code generated by Navicat by clicking on the Script Preview tab:

db.createView("Untitled","actor",[
    {
        $project: {
            _id: 0,
            "full_name": {
                $concat: [
                    "$first_name",
                    ", ",
                    "$last_name"
                ]
            }
        }
    }
])

To see your new view, click on the Preview button or Result tab:

Upon saving a view, Navicat executes the above db.createView command. The aggregation pipeline is saved in the system.views collection. A new document is also saved in the system.views collection for each view created.

Going Forward

Now that we've got the basics down, in the next blog, we'll learn about Collation.

Navicat Blogs
Feed Entries
Blog Archives
Share