August 17, 2023

tMongoDBOutput – Docs for ESB 5.x

tMongoDBOutput

tMongoDBOutput_icon32_white.png

Warning

This component will be available in the Palette of
Talend Studio on the condition that you have subscribed to one of
the Talend
solutions with Big Data.

tMongoDBOutput Properties

Component family

Big Data / MongoDB

 

Function

tMongoDBOutput inserts, updates,
upserts or deletes documents in a MongoDB database collection based
on the incoming flow from the preceding component in the Job.

Purpose

This component executes the action defined on the collection in
the MongoDB database.

Basic settings

Use existing connection

Select this check box and in the Component List click the
relevant connection component to reuse the connection details you already defined.

 

DB Version

List of the database versions.

Available when the Use existing
connection
check box is not selected.

 

Use replica set address

Select this check box to show the Replica
address
table.

In the Replica address table, you
can define multiple MongoDB database servers for failover.

Available when the Use existing
connection
check box is not selected.

 

Server and Port

IP address and listening port of the database server.

Available when the Use existing
connection
or Use replica set
address
check box is not selected.

 

Database

Name of the database.

 

Use SSL connection

Select this check box to enable the SSL encrypted connection.

Then you need to use the tSetKeystore component in the
same Job to specify the authentication information.

For further information about tSetKeystore, see tSetKeystore.

Note that the SSL connection is available only for the version 2.4 + of MongoDB.

 

Required authentication

Select this check box to enable the database
authentication.

 

Username and Password

DB user authentication data.

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.

 

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 data.

Update: update data.

Upsert: update and insert
data.

Delete: delete data.

 

Schema and Edit
Schema

A schema is a row description. It defines the number of fields 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 Studio
User 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 https://help.talend.com/display/KB/Verifying+default+values+in+a+retrieved+schema.

 

Mapping

Specify the parent node for the column in the MongoDB
database.

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.

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 Studio
User 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

The activity of this component can be logged using the log4j feature. For more information on this feature, see Talend Studio User
Guide
.

For more information on the log4j logging levels, see the Apache documentation at http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html.

Limitation

Note

  • The “multi” parameter, which allows to update multiple
    documents at a time, is not supported. Therefore, if two
    documents have the same key, the first is always
    updated, but the second never will.

  • For the update operation, the key cannot be a JSON
    array.

Scenario 1: Creating a collection and writing data to it

This scenario creates the collection blog and
writes post data to it.

Linking the components

  1. Drop tMongoDBConnection, tFixedFlowInput, tMongoDBOutput, tMongoDBClose, tMongoDBInput and tLogRow
    onto the workspace.

  2. 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.

  3. Link tMongoDBConnection to tFixedFlowInput using the OnSubjobOk trigger.

  4. Link tFixedFlowInput to tMongoDBOutput using a Row > Main
    connection.

  5. Link tFixedFlowInput to tMongoDBInput using the OnSubjobOk trigger.

  6. Link tMongoDBInput to tMongoDBClose using the OnSubjobOk trigger.

  7. Link tMongoDBInput to tLogRow using a Row > Main
    connection.

    use_case_tmongodboutput_1.png

Configuring the components

  1. Double-click tMongoDBConnection to open
    its Basic settings view.

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

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

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

  4. Double-click tFixedFlowInput to open its
    Basic settings view.

    use_case_tmongodboutput_3.png

    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:

  5. Double-click tMongoDBOutput to open its
    Basic settings view.

    use_case_tmongodboutput_4.png

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

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

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

    use_case_tmongodboutput_5.png
  7. 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 button_copy_all_from_out_to_in.png to copy all the columns to the input table.

    Click Ok to close the editor.

  8. 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.

  9. Double-click tMongoDBInput to open its
    Basic settings view.

    use_case_tmongodboutput_6.png

    Select the Use existing connection check
    box.

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

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

    use_case_tmongodboutput_7.png
  11. 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.

  12. 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.

  13. 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

  1. Press Ctrl+S to save the Job.

  2. Press F6 to run the Job.

    use_case_tmongodboutput_8.png
  3. 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.

    use_case_tmongodboutput_9.png

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:

Such records can be inserted to the database following the instructions of Scenario 1: Creating a collection and writing data to it.

Linking the components

  1. Drop tMongoDBConnection, tFixedFlowInput, tMongoDBOutput, tMongoDBClose, tMongoDBInput and tLogRow
    from the Palette onto the design
    workspace.

  2. 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.

  3. Link tMongoDBConnection to tFixedFlowInput using the OnSubjobOk trigger.

  4. Link tFixedFlowInput to tMongoDBOutput using a Row > Main
    connection.

  5. Link tFixedFlowInput to tMongoDBInput using the OnSubjobOk trigger.

  6. Link tMongoDBInput to tMongoDBClose using the OnSubjobOk trigger.

  7. Link tMongoDBInput to tLogRow using a Row > Main
    connection.

    use_case_tmongodboutput_10.png

Configuring the components

  1. Double-click tMongoDBConnection to open
    its Basic settings view.

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

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

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

  4. Double-click tFixedFlowInput to open its
    Basic settings view.

    use_case_tmongodboutput_12.png

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

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

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

  5. Double-click tMongoDBOutput to open its
    Basic settings view.

    use_case_tmongodboutput_13.png

    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.

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

    use_case_tmongodboutput_5.png
  7. 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 button_copy_all_from_out_to_in.png to copy all the columns to the input table.

    Click Ok to close the editor.

  8. In the Advanced Settings view, select the
    Generate JSON Document check
    box.

    Select the Remove root node check box.

    In the Data node and Query node fields, enter “data” and “query”.

    use_case_tmongodboutput_14.png
  9. Click the […] button next to Configure JSON Tree to open the configuration
    interface.

    use_case_tmongodboutput_15.png
  10. Right-click the node rootTag and select
    Add Sub-element from the contextual
    menu.

    In the dialog box that appears, type in data for the Data
    node
    :

    use_case_tmongodboutput_16.png

    Click OK to close the window.

    Repeat this operation to define query
    as the Query node.

    Right-click the node data and select
    Set As Loop Element from the contextual
    menu.

    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.

  11. Select all the columns under the Schema
    list
    and drop them to the data node.

    In the window that appears, select Create as
    sub-element of target node
    .

    use_case_tmongodboutput_17.png

    Click OK to close the window.

    Repeat this operation to drop the id
    column from the Schema list under the
    Query node.

  12. Right-click the node id under data and select Add
    Attribute
    from the contextual menu.

    In the dialog box that appears, type in type as the attribute name:

    use_case_tmongodboutput_18.png

    Click OK to close the window.

    Right-click the node @type under
    id and select Set A Fix Value from the contextual menu.

    In the dialog box that appears, type in integer as the attribute value, ensuring the id values are stored as integers in the
    database.

    use_case_tmongodboutput_19.png

    Click OK to close the window.

    Repeat this operation to set this attribute for the id node under Query.

    Click OK to close the JSON Tree
    configuration interface.

  13. Double-click tMongoDBInput to open its
    Basic settings view.

    use_case_tmongodboutput_20.png

    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.

    use_case_tmongodboutput_21.png

    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.

  14. Double-click tLogRow to open its
    Basic settings view.

    use_case_tmongodbrow_8.png

    In the Mode area, select Table (print values in cells of a table for
    better display.

Executing the Job

  1. Press Ctrl+S to save the Job.

  2. Press F6 to run the Job.

    use_case_tmongodboutput_22.png

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


Document get from Talend https://help.talend.com
Thank you for watching.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x