In this exercise, you’ll use IntelliShell to run an aggregate statement that will output the results to a new collection. The statement will remove some of the customers’ personal information, such as their names, email addresses, and dates of birth. You’ll then use this collection in MongoDB Charts to create a column chart based on the sales of a specific user.
To visualize the collection data
- On the IntelliShell tab, replace the existing aggregate statement with the following statement:
db.customers_txn.aggregate( [ { "$unset": [ "title", "first", "last", "email", "dob" ] }, { "$match": { "user_name": { "$exists": true, "$ne": null } } }, { "$out": { "db": "sales", "coll": "customers_clean" } } ] );
The aggregate
statement includes three stages:
- The first stage uses the
$unset
operator to remove thetitle
,first
,last
,email
, anddob
fields. - The second stage uses the
$match
operator to remove any documents whoseuser_name
field is null. - The third stage uses the
$out
operator to save the query results to thecustomers_clean
collection in thesales
database.
- On the IntelliShell toolbar, click the Run button. The query results are displayed in the lower pane, as shown in the following figure.
- In the Connection Tree, expand the sales database node and Collections node, if necessary, and confirm that the customers_clean collection has been created. You might need to refresh the list of collections. You can also open the collection to make certain it’s been created as you expected.
- Sign into the MongoDB website and navigate to the Charts page.
- You must have at least one dashboard in place before you can create a chart. Normally, a dashboard is created by default, but if a dashboard does not exist, you will need to create it now. The following figure shows the Dashboard tab on the Charts page with one dashboard created. The dashboard is named Customer Sales.
- Go to the Data Sources tab and click Add Data Source.
- In the Add Data Sources wizard, select the cluster that contains the
sales
database andcustomers_clean
collection, and then click Next. The wizard will process the request and then display a list of databases and collections. This process can take up to one minute. - When the list appears, select the customers_clean collection node, and then click Finish. The Data Sources tab should now display the collection, along with any other data sources that have been set up. The following figure shows the Data Sources tab with two collections, one of which is sample data provided by MongoDB.
- Return to the Dashboards tab and click the name of the dashboard where you want to add the chart. This opens the dashboard.
- Click the Add Chart button.
- On the Add Chart page, select Column from the Chart Type drop-down list, and then select Grouped, if it’s not already selected.
- From the Data Source drop-down list, select sales.customers_clean. Charts adds the fields to the left pane,
- In the Query text box, paste the following expression:
[ { "$unwind": { "path":"$payments" } } ]
The expression applies the $unwind aggregate operator to the source data to unwind the payments array, which will make it easier to work with the date values in that array.
- Click the Apply button to the right of the text box. MongoDB Charts will apply the expression and update the information in the left pane.
- Select the Filter tab in the middle pane.
- Expand the address field node in the left pane and drag the state field to the + filter placeholder in the Filter section in the middle pane. This loads all the
state
values into the Filter section, where you can choose which states to include in the results. - Clear all checkboxes and then select Washington. If Washington is not listed, click the + Add value link, type Washington in the text box, and click the check icon.
- Click the ^ hide link to minimize the state filter.
- From the left pane, drag the user_name field to the + filter placeholder in the Filter section. This loads all the
user_name
values into the Filter section, where you can choose which usernames to include in the results. - Clear all check boxes and then select bfreemanl. If bfreemanl is not listed, click the + Add value link, type bfreemanl in the text box, and click the check icon.
- Click the ^ hide link to minimize the user_name filter. The middle pane should now look like the following figure.
- Go to the Encode tab in the middle pane.
- From the left pane, drag the user_name field to the + category placeholder in the X Axis area of the Encode section.
- In the Sort by drop-down list in the X Axis area, select CATEGORY. The X Axis settings should now look like the following figure.
- In the left pane, expand the payments node and then drag the value field to the + aggregation placeholder in the Y Axis area of the Encode section. The SUM option should be selected in the Aggregate drop-down list, as shown in the following figure.
At this point, an initial chart will be displayed in the main window, although it still has little meaning.
- From the left pane, drag the date field to the + category placeholder in the Series area of the Encode section. This should enable the Binning option by default, with MONTH selected in the drop-down list.
Configuring the series will update the chart, but it’s still cluttered looking.
- Return to the Filter tab in the middle pane.
- From the left pane, drag the date field to the + filter placeholder in the Filter section. You’ll be presented with several configuration options for setting the date.
- In the date filter, select the Absolute option and then enable the From and To options.
- In the From box, set the date to 01/01/2020, set the time to 12:00:00 AM, and select the Inclusive check box.
- In the To box, set the date to 01/01/2021 and set the time at 12:00:00 AM. Do not select the Inclusive check box.
- Go to the Customize tab in the middle pane.
- In the Axes section, set the Label Angle to HORIZONTAL.
- In the Fields section, select user_name, enable Label Override, and type Customer as the new label.
- In the Fields section, select payments.date, enable Label Override, and type Month key as the new label.
- In the Fields section, select sum ( payments.value ), enable Label Override, and type Total Payments as the new label.
- Add a title and description to the chart, which should now look similar to the following figure.
- Click Save. This will return you to the dashboard, where you can resize the report and view specific information by hovering over the bars or clicking months in the key. The following figure shows the Customer Sales dashboard with the 2020 Sales Report chart.
- Log out of the MongoDB website.
- Return to Studio 3T and close the IntelliShell tab. If prompted to save changes, click No, Discard.
- Close Studio 3T.