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:

  1. Identify the document(s) to update.
  2. Perform the update.

MongoDBAdapter_CustomerData_Example

Click the icon to download a simple example model that shows the usage of the MongoDB adapter in Scheer PAS Designer.

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:

{
"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
}

The simplest way to create a query string is the following:

  1. Create an object having the structure of the document ( Customer in the example).

    create queryData;
  2. Set all query values to this object (the customerID in the example).

    set queryData.id = customerID;
  3. Provide this object as queryString by converting it to JSON using classToExtendedJSON().

    set queryString = queryData.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.

Updating Data

You can update complete documents or only parts of them, depending which operation you use:

OperationNameTypeDescription
updatedocumentAny <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.

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 DatabaseUpdate DocumentNew in Database
{
"name": "John Snow",
"company": "Winter & Partners",
"address": {
"street": "99, Malamute Street",
"city": "Anchorage, AK 99506",
"country:": "USA"
}
}
{
"name": "John Snowflake"
}
{
"name": "John Snowflake",
"company": "Winter & Partners",
"address": {
"street": "99, Malamute Street",
"city": "Anchorage, AK 99506",
"country:": "USA"
}
}
{
"name": "John Snow",
"company": "Winter & Partners",
"address": {
"street": "99, Malamute Street",
"city": "Anchorage, AK 99506",
"country:": "USA"
}
}
{
"address": {
   "city": "Dallas, TX 75043"
}
}
{
"Name": "John Snow"
"company": "Winter & Partners",
"address": {
"city": "Dallas, TX 75043"
}
}

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