Let's say we have objects in CosmosDB with complex nested structure:
[{ "id": "1", "title": "Foo", "metadata": { "metadataId": "123", "fieldValues": [ { "fieldName": "field1", "values": [ "val1" ] }, { "fieldName": "field2", "values": [ "val2" ] }, { "fieldName": "field3", "values": [ "val2" ] } ] } }, ... ]
And we need to fetch items items by metadata values stored in field1, field2, etc. Using above example if we need to fetch objects which have metadata.metadataId = "123", field1.value = "val1" and field2.value = "val2" we should use the following query:
SELECT * FROM c WHERE c.metadata.metadataId = '123' AND EXISTS(SELECT VALUE fv.fieldName FROM fv IN c.dynamicMetadata.fieldValues WHERE fv.fieldName = 'field1' AND EXISTS(SELECT VALUE v FROM v IN fv.values WHERE v = 'val1')) AND EXISTS(SELECT VALUE fv.fieldName FROM fv IN c.dynamicMetadata.fieldValues WHERE fv.fieldName = 'field2' AND EXISTS(SELECT VALUE v FROM v IN fv.values WHERE v = 'val2'))
If values for some field contain multiple values we may construct even more complex conditions by combining conditions for single field values by AND or OR operators depending on requirements.
No comments:
Post a Comment