MongoDB - Update

In this article

Overview

You can use this Snap to update documents in a MongoDB collection. It also supports the upsert capability.

The upstream schema suggest is only supported if the defined collection provides data. MongoDB does not provide a metadata API to describe the collection, so the SnapLogic platform looks at the data and derives the schema from it

mongodb-update-overview2.png

Snap Type

The MongoDB - Update Snap is a Write-type Snap.

Prerequisites

None.

Support for Ultra Pipelines

Works in Ultra Task Pipelines.

Limitations and Known Issues

None.

Snap Views

Type

Format

Number of Views

Examples of Upstream and Downstream Snaps

Description

Type

Format

Number of Views

Examples of Upstream and Downstream Snaps

Description

Input 

Document

  • Min: 1

  • Max: 1

Any Snap with document output view.

A map data to evaluate expression properties and/or to update documents in a MongoDB collection. Each input document produces one document at the output view or the error view.

Output

Document

  • Min: 0

  • Max: 1

Any Snap with document input view.

  • In the above output:

    • updatedDocuments - is the number of MongoDB documents that are changed in the update query. The updateOfExisting is true if updatedDocuments is greater than 0.

    • matchedDocuments - is the number of MongoDB documents that match the query for potential change.

    • updateOfExisting - is true if there are any updatedDocuments, otherwise, false.

For example, if the existing documents are:

And an update with query{} and input document of:

then the result will be as follows:

  • Before introducing the matchedDocuments definition, the updateOfExisting would be true if matchedDocuments was greater than 0. Now, updateOfExisting is true if updatedDocuments is greater than 0.

  • The updateOfExisting output documents would be false if the update query matched documents, but didn’t update documents.

Error

Error handling is a generic way to handle errors without losing data or failing the Snap execution. You can handle the errors that the Snap might encounter while running the Pipeline by choosing one of the following options from the When errors occur list under the Views tab. The available options are:

  • Stop Pipeline Execution: Stops the current pipeline execution when the Snap encounters an error.

  • Discard Error Data and Continue: Ignores the error, discards that record, and continues with the rest of the records.

  • Route Error Data to Error View: Routes the error data to an error view without stopping the Snap execution.

Learn more about Error handling in Pipelines.


Snap Settings

  • Asterisk (*): Indicates a mandatory field.

  • Suggestion icon (): Indicates a list that is dynamically populated based on the configuration.

  • Expression icon (): Indicates whether the value is an expression (if enabled) or a static value (if disabled). Learn more about Using Expressions in SnapLogic.

  • Add icon (): Indicates that you can add fields in the field set.

  • Remove icon (): Indicates that you can remove fields from the field set.

Field Name

Field Type

Description

Field Name

Field Type

Description

Label*

 

Default ValueMongoDB - Update
ExampleMongoDB - Update

String

Specify a name for the Snap. You can modify this to be more specific, especially if you have more than one of the same Snap in your pipeline.

Database name

 

Default Value: N/A
Exampleassets

String/Expression/Suggestion

Specify the database in which the collection is defined. If you do not specify one, the database configured in the MongoDB Account will be used.

 

 

Collection name*

 

Default Value: N/A
Example: leads

String/Expression/Suggestion

Select or enter a MongoDB collection name.

 
 

Update query*

 

Default value: N/A
Example:
(with '=' expression enabled) 

$id represents an integer in this example.

{"id": {$lte: $id}}

or

'{"id": {$lte:' + $id + '}}'

Textbox

The update query represents the update query parameter. It is an expression that evaluates to an object or JSON string. When the expression evaluates to an object, only strict mode is supported. When the expression evaluates to a JSON string, both strict mode and mongo shell mode are supported. Learn more about MongoDB Extended JSON.

 

 

Update operation

Default Value: $set
Example$min

String/Expression/Suggestion

Specify the operation to be performed on the update query.

 

Exclude list

List of input fields to exclude from the dataset.

Exclude field

 

Default Value_id
Example: $ranking

 String/Suggestion

Specify the list of input fields that must be removed from the dataset before sending the dataset to MongoDB.

Array filters

 

Default Value: N/A
Example: $arrayFilters

Expression/Textbox

Specify the filter conditions to determine the array elements you want to modify for the update operation. This field supports upstream values.

Upsert

 

Default Value: Deselected

Checkbox

Select this checkbox to insert a document from the input view if no document in the collection matches the update query criteria. 

 

 

Update all that match

 

Default Value: Selected

Checkbox

Select this checkbox to update all documents in the collection that match the update query criteria; otherwise the Snap updates only one document. 

 

 

Pass through

 

Default value: Selected

Checkbox

Select this checkbox to pass the input document through to the output view under the key 'original'. The document is passed to the Parameter output view, and it will also be passed to the Result Set output view if output is sent from this view.

 

Number of retries

Default Value0
Example: 4

Integer/Expression

Specify the maximum number of attempts to be made to receive a response. The request is terminated if the attempts do not result in a response.

Retry interval (seconds)

Default Value: 1
Example: 5

Integer/Expression

Specify the time interval between two retry requests.

Snap execution

 

Default Value: Execute only
Example: Validate & Execute

 

 

Dropdown list

 

Select one of the three modes in which the Snap executes. Available options are:

  • Validate & Execute: Performs limited execution of the Snap, and generates a data preview during Pipeline validation. Subsequently, performs full execution of the Snap (unlimited records) during Pipeline runtime.

  • Execute only: Performs full execution of the Snap during Pipeline execution without generating preview data.

  • Disabled: Disables the Snap and all Snaps that are downstream from it.

Examples

Update records using array filters

This example pipeline below demonstrates how to use the Array filters field to update records with filter conditions.

Download this pipeline.

Step 1: Configure the MongoDB - Find Snap to search all the records with record id =1.

Step 2: Configure the Mapper Snap with the employees' salaries to be updated for employees, employees_1, and employees_2 elements within the record id=1.

Step 3: Configure the MongoDB - Update Snap with the following filter conditions:

  • {{elem.dept = QA} and {elem.age = 32}}

  • {{elem1.dept = QA} or {elem1.age = 32}}

  • {{elem2.dept = QA} or {elem2.salary <= 25000}}

On validation, you can see that the record with id equals to 1 with multiple elements is updated based on the matched conditions.

Update collection in MongoDB

This example pipeline demonstrates how to update the collection from an upstream Snap to the MongoDB table using the MongoDB Update Snap.

 

Configure the JSON Generator Snap with the data to be updated to the MongoDB collections. 

Configure the MongoDB Update Snap with the collection name, Sample data, to be updated with the values just passed from the upstream JSON Generator Snap. The update query condition 

"{field002:'"+$field002+"'}" is passed (with the expression enabled) to update the documents in the collection.

The successful execution of the pipeline displays the below output preview:


Downloads