You can use the MongoDB adapter to interact with a MongoDB and to insert, get and manipulate documents.
To update documents in a MongoDB database, you can use the update and replace operations.Updating MongoDB documents is twofold:
You can update complete documents or only parts of them, depending which operation you use.
Operation | Name | Type | Description | Link | |
---|---|---|---|---|---|
update | updateString | String | MongoDB update string as valid JSON.
| ||
update | document | Any <document class> | A data structure that contains the new document data. For this, the following rules apply:
| ||
replace | document | Any <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.
Assume you have a customer database containing the following customer document:
{
"id": "0000002b6f7b863000006daa1e5007007a842b9d"
"name": "John Snow",
"company": "Winter & Partners",
"address": {
"street": "99, Malamute Street",
"city": "Anchorage, AK 99506",
"country:": "USA"
},
orderValue: "16323.00
}
The changes that should be applied are propagated to MongoDB via an update string. Either you yourself can build an update string to be used with an update adapter operation, or the xUML Runtime translates a document object you have provided into an update string.
| 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 customer address in the example.
To set values within the update string, create an object of type UpdateCustomer 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 Database | Update String | New in Database | ||
---|---|---|---|---|
{ | 1 |
| { | |
2 |
| { |
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.
Using an update operation with the MongoDB adapter with parameter updateString, you can update all or dedicated properties in the selected document.
create updateAddress; set updateAddress.setOperator.address = <new address object>; set updateString = updateAddress.classToExtendedJSON(); |
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.
Using the update operation of the MongoDB adapter with parameter document, you can update all or dedicated properties in the selected document. Set the values you want to update in the object you provide to the document parameter.
The xUML Runtime will translate the document parameter to an update string. In this case, the same rules apply as for updates via update string, especially the rule concerning properties of complex structures within a document.
Using the replace operation of the MongoDB adapter you can replace a complete document in the database. The document provided with the document 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. |