- Created by Kirstin Seidel-Gebert on Mar 21, 2022
You are viewing an old version of this page. View the current version.
Compare with Current View Page History
Version 1 Next »
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:
MongoDBAdapter_CustomerData_Example
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. Assume we have the following sample Customer document structure: The simplest way to create a query string is the following: Create an object having the structure of the document ( Customer in the example). Set all query values to this object (the customerID in the example). Provide this object as queryString by converting it to JSON using classToExtendedJSON(). To build a query 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 a query string manually (e.g. using concat()) is susceptible to code injection.{
"id": "ebd7c78b-44e0-4cbd-8164-d28431716942"
"name": "John Snow",
"company": "Winter & Partners",
"address": {
"street": "99, Malamute Street",
"city": "Anchorage, AK 99506",
"country:": "USA"
},
orderValue: "16323.00
}create queryData;
set queryData.id = customerID;
set queryString = queryData.classToExtendedJSON();
Updating Data
You can update complete documents or only parts of them, depending which operation you use:
Operation | Name | Type | Description |
---|---|---|---|
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.
Updating a Document Using Parameter "Document"
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.
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 Document | New in Database |
---|---|---|
{ | { | { |
{ | { | { |
Providing only the city with the document will generate an update string that updates the complete address. All properties of address that are not listed will be removed from the target document.
Replacing 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.
- No labels