In this exercise, you’ll use IntelliShell to run an aggregate
statement that will output the results to a new collection. You’ll then export the collection to a .csv file, open the file in Microsoft Excel, and save it as an .xlsx file, which ensures that all visualization features are preserved when saving the file.
To export the collection as a .csv file
- In the Connection Tree, expand the sales database node and, if necessary, expand the Collections node.
- Right-click the customers_txn collection node, and then click Open IntelliShell. Studio 3T adds the IntelliShell tab to the main window. By default, Studio 3T defines a basic
find
statement on thecustomers_txn
collection object, but you’ll be replacing this statement with anaggregate
statement. - Replace the
find
statement with the following aggregate statement:
db.customers_txn.aggregate( [ { "$match": { "payments.date": { "$lt": ISODate("2019-01-01T00:00:00.000Z" ) } } }, { "$replaceRoot": { "newRoot": { "state": "$address.state", "city": "$address.city", "value": "$payments.value" } } }, { "$unwind": { "path" : "$value" } }, { "$group": { "_id": { "state": "$state", "city": "$city"}, "total": { "$sum": { "$toInt": "$value" } } } }, { "$out" : { "db" : "sales", "coll" : "customers_hx" } } ] );
The aggregate
statement includes five stages:
- The first stage uses the
$match
operator to return only those documents with apayments.date
value prior to January 1, 2019. - The second stage uses the
$replaceRoot
operator to replace each document in the pipeline with the embedded document specified in the expression. Thestate
field in the new documents is based on the original$address.state
field, the newcity
field is based on the original$address.city
field, and the newvalue
field is based on the original$payments.value
field. Thevalue
field is created as an array because the original field is part of an array. - The third stage uses the
$unwind
operator to deconstruct the values in thevalue
array and output a document for each array element. - The fourth stage uses the
$group
operator to group the data by thestate
field and then by thecity
field. The stage also calculates the total amount of payments for each state/city combination. - The fifth stage uses the
$out
operator to save the query results to thecustomers_hx
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.
The results show the total amount of sales for each state/city pair. The results are shown in Table View, with all the embedded fields displayed. Because the aggregate
statement includes the $out
stage, MongoDB automatically creates the specified collection (customers_hx
).
- In the Connection Tree, expand the sales database node and Collections node, if necessary. The customers_hx collection node should now be listed under the Collections node.
- Right-click the customers_hx collection node and click Export Collection.
- On the Select the export format pane of the Export wizard, select CSV and click Configure. Studio 3T opens the Export tab in the main window and displays a message box stating that the scan is finished, as shown in the following figure.
- Click Dismiss to close the message box. The Export unit #1 – CSV tab should now be active.
- In the File text box, type a target path for where to save the file. You can also click the folder icon and navigate to the target folder. By default, the file is named customers_hx.csv, which is the name you should use for this exercise.
At this point, you can also configure other settings, such as which fields to export or which delimiter to use, but in this case, the default settings are fine.
- On the toolbar near the top of the Export tab, click the Run button.
- In the Confirm Export message box, click OK. Studio 3T generates the .csv file.
- Close the Export tab. If prompted to save configuration changes, click Discard changes.
- Navigate to the target folder, open the customers_hx.csv file in Microsoft Excel, and save the file as an .xlsx document.
- You can now use Excel’s many features to organize the data. For example, the following figure shows the sales data after it has been formatted and sorted.
Notice that the Total
values are now treated as currency and that the data is sorted first by state and then by city. In addition, the column names have been updated, with the names centered against a green background.
You can also filter the data and create charts. For example, you can filter out all data except what applies to the states Arizona, Colorado, New Mexico, and Utah (the Four Corners states). From this, you can create a corresponding treemap chart, as shown in the following figure.
Excel keeps the table and chart in sync. If you add or remove data in the table, Excel will automatically update the chart.
You can also create pivot tables and pivot charts in Excel. Here’s an example of the two based on the same sales data used for the treemap chart.
Excel offers numerous features for visualizing data, all of which can be based on data from a .csv file.
- Save the .xlsx file and exit Excel.
- Leave Studio 3T and the IntelliShell tab open for the next exercise.