0

I am trying to query document which is embedded to get the results as follows:

Document:

{
    "id": "1",
    "description": "coco cola",
    "locations": [
        { "locationId": "14", "taxType" : "20", "state": "Florida" },
        { "locationId": "14", "taxType" : "22", "state": "Florida" },
        { "locationId": "16", "taxType" : "23", "state": "California" }
      ]  
},
{
    "id": "2",
    "description": "Mac & Cheese",
    "locations": [
        { "locationId": "22", "taxType" : "31", "state": "Texas" },
        { "locationId": "16", "taxType" : "23", "state": "California" }
      ]  
}

I am trying to query above document using SQL query:

SELECT * FROM c
where c.id= "1" and ARRAY_CONTAINS(c.locations,{"locationId": "14"},true)

Expected output: (as we are using where locationId = 14 we need to ignore locationId = 16 for Id = 1):

{
    "id": "1",
    "description": "coco cola",
    "locations": [
        { "locationId": "14", "taxType" : "20", "state": "Florida" },
        { "locationId": "14", "taxType" : "22", "state": "Florida" }
      ]  
}

ARRAY_CONTAINS doesn't work and I am not getting expected output, it gives me empty result when I use above query. How can I query with where clause on the embedded documents?

1 Answer 1

1

I think your expected output can't be achieved by SQL. You can use UDF and pay attention to it's cost. If you think cost of UDF is too expensive, you can transform this on your client side.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.