We Use Cookies

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with this.

See our cookie policy.

Automation Action: Update MongoDB

Create or update MongoDB documents.

Built-In Action

Insert, Update or Delete documents in a MongoDB collection. This action can update any local MongoDB, or cloud based MongoDB compatible document databases including Amazon DocumentDB, Azure Cosmos and MongoDB Atlas.

Specify the MongoDB Connection String, Database Name & Collection Name.

Select the Operation:

  • Insert: To insert a new document.
  • Upsert: If you want an existing document returned from the Query to be updated. If no document is returned from the Query then a new document will be inserted.
  • Update: To Update one or more existing documents.
  • Delete: To delete one or more existing documents.

If Upsert, Update or Delete is selected you must specify the Query Json. See: Query Documents — MongoDB Manual for query syntax. You can use %variable% replacements in the query and document json. If the value is a string then it must be enclosed in quotes. For example:


{ status: "%statuscode%" }              

Click the Test Query button to test the connection and query. A maximum of 100 documents will be returned when using the Test option.

Enter Document Json for Insert, Upsert or Update.

If Update or Delete is selected then you can enable All Matching Documents. If enabled then all documents returned from the query will be updated/deleted.

Example Update:

With Query set to:


{ "Name" : { "$gt" : "A" } }              

And Document Json set to:


{
    "$set" : {
        "Downloads" : 11
    }
}              

If All Matching Documents is enabled this would set the 'Downloads' field to 11 for all documents with 'Name' field greater than 'A'. If All Matching Documents not enabled then the first document found would be updated.

If you want to store a full copy of the incoming message you can use the built-in variable %Msg_Json%. If the incoming message body is already Json (for example, if using the Database message source) then you can set the Document Json to %Msg_Body%.

You can use the Create Json action to create a Json document to insert/update.

You can assign the result of the operation to a variable. Select from the Assign Result To list. For Update/Delete operations the result will be the number of documents affected. For Insert/Upsert operations the result will be the _id of the Inserted/Updated document.