Exercise 2: Exporting document data to a .json file

In this exercise, you’ll create a simple query that limits the pubs in the british_pubs collection to those located in Scotland. You’ll then export the query results to a .json file.

To export the documents

1. On the british_pubs collection tab, select the Table View option.

2. Double-click any value in the address field.

The Result tab now displays the fields in the embedded document associated with each document’s top-level address field, as shown in the following figure.

Table view

3. Near the top right corner of the collection tab, click on Visual Query Builder to open it.

4. On the Result tab, locate one of the Scotland values in the Region column and drag the value to the first text box in the Query section of the query form. (The text box tip reads Drag field here or double-click.)

Your query form should look similar to the following figure.

Query tab

5. In the Query section of the query form, click the Run button.

The Result tab should now show only Scottish pubs.

6. Click on Hide Query Builder to close it.

7. Click Export on the toolbar to launch the Export Wizard.

toolbar

8. On the Export source page of the Export Wizard, select the Current Query Result option, and then click Next.

export tab

9. On the Select the export format page, select JSON and then click Finish.

Export tab json

10. This will open the Export unit #1 – JSON tab, seen below.

export json tab

Here we’ll do a couple of things:

  • Under Format options, choose JSON – mongo shell / Studio 3T
  • Under Select target, choose File

You can also choose to include commas between documents or export the documents as an array, depending on how you’ll use the export file, under Other options.

11. Click the folder icon to the right of the File text box.

12. In the file manager text box, type scottish_pubs.json in the Save As text box.

13. Navigate to the target folder, and then click Save.

14. Browse through the Output Preview section to check that the export looks correct.

Output preview

15. On the top toolbar, click on Execute to run the export.

Export tab

16. Verify that the scottish_pubs.json export file has been created. Right-click on the export in the Operations window and choose Open in File Explorer (Windows and Linux) or Reveal in Finder (macOS).

open file explorer

You can use a text editor to confirm that the file contains the exported documents.

17. Leave the british_pubs collection tab as is for the next exercise.

Log in

Log in to your Studio 3T Academy account

Forgot password?

Don't have an account yet? Sign up
Sign up

Start learning MongoDB today. All fields are mandatory.

By signing up for a course, you agree to the 3T Software Labs Privacy Policy.


Already have an account? Log in