The Authentication mechanism drop-down list becomes
available. Among the listed mechanisms, the NEGOTIATE one
is recommended if you are not using Kerberos, because it automatically select the
authentication mechanism the most adapted to the MongoDB version you are using.

For details about the other mechanisms in this list, see MongoDB Authentication from the MongoDB documentation.

Set Authentication database

If the username to be used to connect to MongoDB has been created in a specific
Authentication database of MongoDB, select this check box to enter the name of this
Authentication database in the Authentication database
field that is displayed.

To enter the password, click the [...] button next to the
password field, and then in the pop-up dialog box enter the password between double quotes
and click OK to save the settings.

Available when the Required
authentication check box is selected.

If the security system you have selected from the Authentication
mechanism drop-down list is Kerberos, you need to enter the User principal, the Realm and
the KDC server fields instead of the Username and the Password fields.

Collection

Name of the collection in the MongoDB database.

Drop collection if exist

Select this check box to drop the collection if it already
exists.

Action on data

The following operations are available:

Insert: insert documents.

Set: modifies the existing fields of an existing
document and appends a field if it does not exist in this document.

If you need to apply this action on all the documents in the collection to be
used, select the Update all document check box that
is displayed; otherwise, only the first document is updated.

Update: replaces the existing documents with the
incoming data but keeps the technical ID of these documents.

Upsert: inserts a document if it does not exist
otherwise it applies the same rules as Update.

Upsert with set: inserts a document if it does
not exist otherwise it applies the same rules as Set

If you need to apply this action on all the documents in the collection to be
used, select the Update all document check box that
is displayed; otherwise, only the first document is updated.

Delete: delete documents.

Schema and Edit
Schema

A schema is a row description. It defines the number of fields (columns) to be processed
and passed on to the next component. The schema is either Built-In or stored remotely in the Repository.

Click Edit schema to make changes to the schema. If the
current schema is of the Repository type, three options are
available:

View schema: choose this option to view the
schema only.

Change to built-in property: choose this option
to change the schema to Built-in for local
changes.

Update repository connection: choose this option
to change the schema stored in the repository and decide whether to propagate the
changes to all the Jobs upon completion. If you just want to propagate the changes
to the current Job, you can select No upon
completion and choose this schema metadata again in the [Repository Content] window.

Click Sync columns to retrieve the
schema from the previous component connected in the Job.

Built-In: You create and store the schema locally for
this component only. Related topic: see Talend StudioUser Guide.

Repository: You have already created the schema and
stored it in the Repository. You can reuse it in various projects and Job designs. Related
topic: see Talend Studio User Guide.

When the schema to be reused has default values that are integers or functions, ensure
that these default values are not enclosed within quotation marks. If they are, you must
remove the quotation marks manually.

For more details, see the article Verifying default values in a retrieved schema on
Talend Help Center (https://help.talend.com).

Mapping

Each column of the schema defined for this component represents a field of the documents
to be read. In this table, you need to specify the parent nodes of these fields, if
any.

The
first and the last
fields have person as their parent node but the _id field does not have any parent node. So once completed, this
Mapping table should read as
follows:

Column Parent node path
_id
first "person"
last "person"

Not available when the Generate JSON
Document check box is selected in Advanced settings.

Die on error

This check box is cleared by default, meaning to skip the row on error
and to complete the process for error-free rows.

Advanced settings

Generate JSON Document

Select this check box for JSON configuration:

Configure JSON Tree: click the
[...] button to open the interface
for JSON tree configuration. For more information, see Configuring a JSON Tree.

Group by: click the [+] button to add lines and choose the input
columns for grouping the records.

Remove root node: select this check
box to remove the root node.

Data node and Query node (available for update and upsert actions):
type in the name of data node and query node configured on the JSON
tree.

Warning

These nodes are mandatory for update and upsert actions. They are
intended to enable the update and upsert actions though will not be
stored in the database.

No query timeout

Select this check box to prevent MongoDB servers from stopping idle
cursors at the end of 10-minute inactivity of these cursors. In this
situation, an idle cursor will stay open until either the results of
this cursor are exhausted or you manually close it using the
cursor.close() method.

A cursor for MongoDB is a pointer to the result set of a query. By
default, that is to say, with this check box being clear, a MongoDB
server automatically stops idle cursors after a given inactivity period
to avoid excess memory use. For further information about MongoDB
cursors, see https://docs.mongodb.org/manual/core/cursors/.

tStatCatcher Statistics

Select this check box to collect the log data at the component
level.

Global Variables

NB_LINE: the number of rows read by an input component or
transferred to an output component. This is an After variable and it returns an
integer.

ERROR_MESSAGE: the error message generated by the
component when an error occurs. This is an After variable and it returns a string. This
variable functions only if the Die on error check box is
cleared, if the component has this check box.

A Flow variable functions during the execution of a component while an After variable
functions after the execution of the component.

To fill up a field or expression with a variable, press Ctrl +
Space to access the variable list and choose the variable to use from it.

For further information about variables, see Talend StudioUser Guide.

Usage

tMongoDBOutput executes the action
defined on the collection in the MongoDB database based on the flow
incoming from the preceding component in the Job.

Log4j

If you are using a subscription-based version of the Studio, the activity of this
component can be logged using the log4j feature. For more
information on this feature, see Talend Studio User Guide.

Linking the components

Rename tFixedFlowInput as blog_post_data, tMongoDBOutput as write_data_to_collection, tMongoDBInput as read_data_from_collection and tLogRow as show_data_from_collection.

Link tMongoDBConnection to tFixedFlowInput using the OnSubjobOk trigger.

Link tFixedFlowInput to tMongoDBOutput using a Row > Main
connection.

Link tFixedFlowInput to tMongoDBInput using the OnSubjobOk trigger.

Link tMongoDBInput to tMongoDBClose using the OnSubjobOk trigger.

Link tMongoDBInput to tLogRow using a Row > Main
connection.

Configuring the components

Double-click tMongoDBConnection to open
its Basic settings view.

From the DB Version list, select the
MongoDB version you are using.

In the Server and Port fields, enter the connection details.

In the Database field, enter the name of the MongoDB
database.

Double-click tFixedFlowInput to open its
Basic settings view.

Select Use Inline Content (delimited
file) in the Mode
area.

In the Content field, enter the data to write to the
MongoDB database, for example:

1;Andy;Open Source Outlook;Open Source,Talend;Talend, the leader of the open source world...
3;Andy;ELT Overview;ELT,Talend;Talend, the big name in the ELT circle...
2;Andy;Data Integration Overview;Data Integration,Talend;Talend, the leading player in the DI field...

Double-click tMongoDBOutput to open its
Basic settings view.

Select the Use existing connection and
Drop collection if exist check
boxes.

In the Collection field, enter the name
of the collection, namely blog.

Click the [...] button next to Edit schema to open the schema editor.

Click the [+] button to add five columns
in the right part, namely id, author, title, keywords and
contents, with the type as Integer and String respectively.

Click to copy all the columns to the input table.

Click Ok to close the editor.

The columns now appear in the left part of the Mapping area.

For columns author, title, keywords and
contents, enter their parent node
post. By doing so, those nodes reside
under the node post in the MongoDB
collection.

Double-click tMongoDBInput to open its
Basic settings view.

Select the Use existing connection check
box.

In the Collection field, enter the name
of the collection, namely blog.

Click the [...] button next to Edit schema to open the schema editor.

Click the [+] button to add five columns,
namely id, author, title, keywords and contents, with the type as Integer and String
respectively.

Click OK to close the editor.

The columns now appear in the left part of the Mapping area.

For columns author, title, keywords and contents,
enter their parent node post so that the
data can be retrieved from the correct positions.

In the Sort by area, click the [+] button to add one line and enter id under Column.

Select asc from the Order asc or desc? column to the right of the id column. This way, the retrieved records will
appear in ascending order of the id
column.

Executing the Job

Press Ctrl+S to save the Job.

Press F6 to run the Job.

Switch to the database talend and read data from the
collection blog in the MongoDB command
line client. You can find that author,
title, keywords and contents all
reside under the node post. Meanwhile,
the records are stored in the same order as the source input.

Scenario 2: Upserting records in a collection

This scenario upserts the collection blog as an
existing record has its author changed and a new record is added. Before the upsert, the
collection blog looks like:

1;Andy;Open Source Outlook;Open Source,Talend;Talend, the leader of the open source world...
2;Andy;Data Integration Overview;Data Integration,Talend;Talend, the leading player in the DI field...
3;Andy;ELT Overview;ELT,Talend;Talend, the big name in the ELT circle...

Linking the components

Rename tFixedFlowInput as blog_post_data, tMongoDBOutput as write_data_to_collection, tMongoDBInput as read_data_from_collection and tLogRow as show_data_from_collection.

Link tMongoDBConnection to tFixedFlowInput using the OnSubjobOk trigger.

Link tFixedFlowInput to tMongoDBOutput using a Row > Main
connection.

Link tFixedFlowInput to tMongoDBInput using the OnSubjobOk trigger.

Link tMongoDBInput to tMongoDBClose using the OnSubjobOk trigger.

Link tMongoDBInput to tLogRow using a Row > Main
connection.

Configuring the components

Double-click tMongoDBConnection to open
its Basic settings view.

From the DB Version list, select the
MongoDB version you are using.

In the Server and Port fields, enter the connection details.

In the Database field, enter the name of the MongoDB
database.

Double-click tFixedFlowInput to open its
Basic settings view.

Select Use Inline Content (delimited
file) in the Mode
area.

In the Content field, enter the data for upserting the
MongoDB database, for example:

1;Andy;Open Source Outlook;Open Source,Talend;Talend, the leader of the open source world...
2;Andy;Data Integration Overview;Data Integration,Talend;Talend, the leading player in the DI field...
3;Anderson;ELT Overview;ELT,Talend;Talend, the big name in the ELT circle...
4;Andy;Big Data Bang;Big Data,Talend;Talend, the driving force for Big Data applications...

As shown above, the 3rd record has its author changed and the 4th record
is new.

Double-click tMongoDBOutput to open its
Basic settings view.

Select the Use existing connection and
Die on error check boxes.

In the Collection field, enter the name
of the collection, namely blog.

Select Upsert from the Action on data list.

Click the [...] button next to Edit schema to open the schema editor.

Click the [+] button to add five columns
in the right part, namely id, author, title, keywords and
contents, with the type as Integer and String respectively.