Define a collection owner with a specific target
This example shows how to use the targetProperty of a collection owner to extract a specific field from the updated document instead of the whole record.
Set
scopetoFULL_DOCUMENTwhen using thetargetPropertyattribute.Use
watchListto monitor specific document properties independently from the extracted field.Set
targetPropertywith a JSON path to store only a sub-field of the document in the value.Trigger a callback script with the extracted field value on each relevant document update.
git checkout origin/osp-scripts-configuration .
git checkout origin/osp-collections-configuration .
git checkout origin/example-collections-owner-with-target .
Steps
1. Define a new value
Create a
value.osppfile with a BOOLEAN type. In this value, we will attempt to retrieve the status of the device (status field). But you can change the type of the value according to the property you want to retrieve from the collection:root/collections/owner/value.ospp{ "name": "Device_Status", "description": "Last status of the Device", "type": "BOOLEAN" }
2. Define a collection owner to update the value
A collection owner is linked to a collection by specifying:
schemaId: the id of the collection.
filterId: the id of the filter. If the updated document matches this filter, the value will be updated.
Additionally, we set the following attributes:
scope: the scope must be
FULL_DOCUMENTwhen using the targetProperty attributewatchList: a list of properties to monitor for changes. If empty, the whole document will be monitored. It can be different from the targetProperty as showcased here.
And finally, we set the targetProperty attribute with a JSON path to choose which attribute of the document we want to store in the value. Then resulting
owner.collectionsfile should look like this:root/collections/owner/owner.collections{ "schemaId": "root.collections.device", "filterId": "filter_all", "scope": "FULL_DOCUMENT", "watchList": [ "ip_address" ], "targetProperty": "$.status", "type": "ON_COLLECTION" }
3. Write a JS script to call with the content of the updated value
In this example, we use a script to do something when the value is updated. For that, we need to have an
output.scripts:root/collections/on_update/output.scripts{ "scriptId": "root.collections.on_update" }This output is linked to this
detached.scripts:root/collections/on_update/detached.scripts{ "moduleId": "modules.scripts.scripts-1", "sourceFile": "root/collections/on_update/device_updated.js", "accessedValues": [] }Which is linked to this script:
root/collections/on_update/device_updated.jsfunction main() { let status = JSON.parse(trigger.content); log.info("Updated device status : " + status + ""); //do something } main();
4. Define a callback for the value
The last step is to define a callback to link the value and the script output.
root/collections/owner/callback.ospp{ "linkedOutputs": [ { "outputId": "root.collections.on_update" } ] }