Skip to main content
Skip table of contents

Updating MongoDB Documents

This page explains the MongoDB Adapter in Bridge context. If you were looking for the same information regarding the PAS Designer, refer to MongoDB Adapter in the Designer guide.

Use stereotype <<MongoDBAdapter>> on an action node to interact with a MongoDB and to insert, get and manipulate documents.

Example File (Builder project Add-ons/MongoDB):

<your example path>\Add-ons\MongoDB\uml\simpleMongoDbAccess.xml

To update documents in a MongoDB database, you can use the update and replace actions. The example above shows a replace action. Parameter queryString identifies the document to be replaced, document supplies the new document.

Finding the Documents to Update

For all actions that refer to existing documents, you need to provide a query string (queryString) to identify them. A query string contains all properties of the document you want to use for selection.
The simplest way to create a query string is to create an object having the structure of the document (queryData in the example above), and set all query values to this object.

Then, provide this object as queryString by converting it to JSON using classToExtendedJSON().

Updating Data

You can update complete documents or only parts of them, depending on the input you give to the adapter.

ActionNameTypeDescription
updateupdateStringString

MongoDB update string as valid JSON.

In contrast to the MongoDB shell, the JSON keys must be quoted properly.

documentAny <document class>

A data structure that contains the new document data. For this, the following rules apply:

  • unset top-level fields will be skipped
  • complex fields will be replaced entirely
replacedocumentAny <document class>A data structure representing the document you want to replace the selected document with. The old stored document will be replaced by the new one, the MongoDB _id, however, will not change.

The following examples refer to the same MongoDB document structure as shown above.

Building an Update String

Assume you have a customer database containing the following customer document:

{
"name": "John Snow",
"company": "Winter & Partners",
"address": {
"street": "99, Malamute Street",
"city": "Anchorage, AK 99506",
"country:": "USA"
}
}

The changes that should be applied are propagated to MongoDB via an update string. Either you yourself build an update string to be used with an update adapter action, or the xUML Runtime translates a document object you have provided into an update string.

CODE
{
	"$set": {
		"name": "John Snowflake"
	}
}
A valid update string contains a $set key providing as value the changes to be applied.

To build an update string, we recommend to not use concat() operations but to create a data structure that represents the update string and can be converted to JSON with classToExtendedJSON().

Building an update string manually (e.g. using concat()) is susceptible to code injection.

Find below the class structure that represents an update string for the customers example.


  • As a class property cannot have a name $set,  you need to apply stereotype <<E2EAttribute>> and an external name $set.
  • The structure below the $set key reflects the document structure (Customer) of a customer.

To set values within the update string, create an object of type UpdateCompleteAddress and assign values to the properties you want to change.

If you want to update parts of a sub-structure, e.g. the customer address, you need to be careful. Have a look at the following example:

Currently in DatabaseUpdate StringNew in Database
{
"name": "John Snow",
"company": "Winter & Partners",
"address": {
"street": "99, Malamute Street",
"city": "Anchorage, AK 99506",
"country:": "USA"
}
}
1

CODE
{
	"$set": {
		"address": {
			"city": "Dallas, TX 75043"
		}
	}
}
{
"Name": "John Snow"
"company": "Winter & Partners",
"address": {
"city": "Dallas, TX 75043"
}
}
2

CODE
{
	"$set": {
		"address.city": "Dallas, TX 75043"
	}
}
{
"Name": "John Snow",
"company": "Winter & Partners",
"address": {
"street": "99, Malamute Street",
"city": "Dallas, TX 75043"
"country:": "USA"
}
}

Using update string 1 you will update the complete address. All properties of address that are not listed in the statement will be removed from the target document.

This is also the way the Runtime will translate any given update document.

Using update string 2 only the city property of the target document will be updated. All other properties will be left intact.

Updating a Document Via an Update String

Using action update with the MongoDB adapter with parameter updateString, you can update all or dedicated properties in the selected document.

In this example, the address part of a customer document is updated. Depending on the structure of the update string, the complete address is replaced, or single address properties are updated.
Refer to Building an Update String for more information on how to build an update string, and an example of the implications.

Updating a Document Using Parameter "Document"

Using action update with the MongoDB adapter with parameter document, you can update all or dedicated properties in the selected document.

The provided document will be translated to an update string by the xUML Runtime. In this case, the same rules apply as for updates via update string, especially the rule concerning properties of complex structures within a document.

Replacing a Document

Using action replace with the MongoDB adapter you can replace a complete document in the database.

The document provided with the parameter replaces the document indicated by the query string as is. The MongoDB _id, however, stays intact.

The structure of the old document does not necessarily need to match the structure of the new document.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.