Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

Academy 3T

  • Explore our courses
    • MongoDB 101: Getting Started
    • MongoDB 201: Querying MongoDB Data
    • MongoDB 301: Aggregation
  • Get certified

Exercise 2: Exporting a collection as a .csv file for use by a third-party tool

MongoDB 301: Aggregation Reporting with Studio 3T Aggregations Exercise 2: Exporting a collection as a .csv file for use by a third-party tool

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.

If you don’t have Microsoft Excel installed on your system when working through this exercise, complete the exercise through Step 12 and then use another application to access the data in the .csv file. The idea here is to demonstrate how easy it is to export your collection to a .csv file, which provides a universal format for providing data to those who need it.

To export the collection as a .csv file

  1. In the Connection Tree, expand the sales database node and, if necessary, expand the Collections node. 
  2. 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 the customers_txn collection object, but you’ll be replacing this statement with an aggregate statement. 
  3. 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 a payments.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. The state field in the new documents is based on the original $address.state field, the new city field is based on the original $address.city field, and the new value field is based on the original $payments.value field. The value 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 the value array and output a document for each array element.
  • The fourth stage uses the $group operator to group the data by the state field and then by the city 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 the customers_hx collection in the sales database.
  1. 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).

  1. 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.
  2. Right-click the customers_hx collection node and click Export Collection.
  3. 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.
  1. Click Dismiss to close the message box. The Export unit #1 – CSV tab should now be active.
  2. 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.
  1. On the toolbar near the top of the Export tab, click the Run button.
  2. In the Confirm Export message box, click OK. Studio 3T generates the .csv file.
  3. Close the Export tab. If prompted to save configuration changes, click Discard changes.
  4. Navigate to the target folder, open the customers_hx.csv file in Microsoft Excel, and save the file as an .xlsx document.
  5. 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. 

Refer to the Excel documentation for details on how to format data and add visualizations to your worksheets. You can also use the .csv file with applications other than Excel. For example, you can use a data visualization tool such as Microsoft Power BI Desktop. Many applications will let you import a .csv file.

  1. Save the .xlsx file and exit Excel.
  2. Leave Studio 3T and the IntelliShell tab open for the next exercise.

Previous Topic
Back to Lesson
Next Topic
  • Course Home Expand All
    Building a Basic Aggregation
    4 Topics | 1 Quiz
    Exercise 1: Filtering the documents in the aggregation pipeline
    Exercise 2: Grouping the documents in the aggregation pipeline
    Exercise 3: Sorting the documents in the aggregation pipeline
    Exercise 4: Adding processing options to the aggregation
    Building a Basic Aggregation: Test your skills
    Introducing the Aggregation Editor
    4 Topics | 1 Quiz
    Exercise 1: Importing an aggregate statement into the Aggregation Editor
    Exercise 2: Replace a field in the aggregation pipeline
    Exercise 3: Reorder the fields in the aggregation pipeline
    Exercise 4: Changing the sort order in the aggregation pipeline
    Introducing the Aggregation Editor: Test your skills
    Working with Arrays in the Aggregation Pipeline
    5 Topics | 1 Quiz
    Exercise 1: Using expression operators to filter input documents
    Exercise 2: Unwinding an array to create individual documents
    Exercise 3: Grouping array values and generating a document count for each group
    Exercise 4: Writing pipeline results to a new collection
    Working with Arrays in the Aggregation Pipeline: Test your skills
    MongoDB 301 Mid-Course Feedback
    Adding Lookup Data to the Aggregation Pipeline
    4 Topics | 1 Quiz
    Exercise 1: Adding lookup data to the aggregation pipeline
    Exercise 2: Converting string values in one of the lookup fields to integers
    Exercise 3: Adding a computed ratio field based on the converted lookup field
    Exercise 4: Limiting the number of returned documents
    Adding Lookup Data to the Aggregation Pipeline: Test your skills
    Working with Reschema for MongoDB
    4 Topics | 1 Quiz
    Exercise 1: Setting up a reschema unit that includes lookup data
    Exercise 2: Defining a target collection in the reschema unit
    Exercise 3: Adding and scheduling a task to create the target collection
    Exercise 4: Running an aggregate statement against the target collection
    Working with Reschema for MongoDB: Test your skills
    Reporting with Studio 3T Aggregations
    3 Topics | 1 Quiz
    Exercise 1: Creating a view based on an aggregation query
    Exercise 2: Exporting a collection as a .csv file for use by a third-party tool
    Exercise 3: Visualizing collection data in MongoDB Charts
    Reporting with Studio 3T Aggregations: Test your skills
    Course Extras
    Return to MongoDB 301: Aggregation
  • Studio 3T

    MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

    Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

    Education

    • Free MongoDB Tutorials
    • Connect to MongoDB
    • Connect to MongoDB Atlas
    • Import Data to MongoDB
    • Export MongoDB Data
    • Build Aggregation Queries
    • Query MongoDB with SQL
    • Migrate from SQL to MongoDB

    Resources

    • Feedback and Support
    • Sales Support
    • Knowledge Base
    • FAQ
    • Reports
    • White Papers
    • Testimonials
    • Discounts

    Company

    • About Us
    • Blog
    • Careers
    • Legal
    • Press
    • Privacy Policy
    • EULA

    © 2023 3T Software Labs Ltd. All rights reserved.

    • Privacy Policy
    • Cookie settings
    • Impressum

    We value your privacy

    With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data.

    By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
    Accept all
    Decline all
    Manage cookies
    ✕

    Privacy Preference Center

    With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in our Privacy Policy.

    Accept all cookies

    Manage consent preferences

    Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

    Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

    Google Analytics

    Google Ads

    Bing Ads

    Facebook

    LinkedIn

    Quora

    Hotjar

    Reddit

    Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

    HubSpot

    Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

    Accept selected