In this exercise, you’ll add a target collection to the reschema unit that you set up in the previous exercise. You’ll then modify the schema in the target collection by adding, changing, and removing fields.
To define the target collection
- On the Reschema unit #1 tab of the Reschema window, click the Add target collection option above the top right panel. This launches the Add target collection wizard, with the Select target collection panel displayed, as shown in the following figure. The panel lists your connection’s databases.
- Expand the sales database node, select the Collections node, and click the Add collection option on the toolbar.
- In the Add New Collection dialog box, type customers_merged, and then click OK. This returns you to the Select target collection panel.
- Click Next to advance the wizard to the Select fields to add panel.
- In the field list, expand the
address
field and clear the check boxes associated with thestreet
field andzip_code
field. The field list should now look like the following figure.
- Click Finish. This returns you to the Reschema unit #1 tab. The top right panel should now include the schema for the target collection (customers_merged). The collection itself has not yet been created, only the schema definition. The following figure shows the top right panel with the new schema.
The bottom panel provides a sample of the documents that will be added to the collection when it is created. The following figure shows the first document in the collection.
As you can see, the document includes the services
array. In addition, the address
field now includes only the city
and state
fields.
- Click the Add new field button above the top right panel. This launches the Add new field dialog box, shown in the following figure. You’ll be adding an empty field configured with the
Object
data type.
- In the Select field name text box, type contact.
- Ensure that the Create blank new field option is selected.
- From the Select field type drop-down list, select Object, and then click OK. The new field is added to the list of fields in the top right panel.
- Drag the
contact
field up the list and drop it below the_id
field socontact
is the second field. - Drag the following fields up the list and drop them on the contact field to make them embedded fields:
title
first
last
email
user_name
Double-click the user_name field (to change its name), type username in the active box, and then press Enter. The contact field should now look like the following figure.
- Right-click the
address
field and then click Flatten selected object/array. Studio 3T moves thecity
andstate
fields to the top schema level and removes theaddress
field. - Drag the
services
field up the list and drop it just below thestate
field. - Right-click the
services
field and then click Change type. You’ll be changing the type toObject
in preparation for simplifying the field’s structure. - In the Change type dialog box, select Object from the New field type drop-down list, and then click OK.
- Expand the services field node, right-click the
0
value, and click Flatten selected object/array. Studio 3T changes the services field to a simple embedded document. - Expand the
services
field if necessary, right-click the_id
field, and click Delete selected field. Repeat the process for theemail
field. Theservices
field should now look like the following figure.
You could have removed the _id
and email
fields when you first added the target collection, instead of waiting until now. This step was included here to demonstrate how easy it is to remove a field at any time during the building process.
- Leave the Reschema tab and reschema unit in place for the next exercise.