0

I'm trying to write a cosmos query that return a list of ids, which contain duplicates in a string array that exists on each record.

Here's an example of the dataset I'm working with. using this data, only the id of A and B should be returned as their "things" array contains at least one duplicate strings.


[
    {
        "id": "A",
        "things": [
            "1",
            "1",
            "2",
            "2",
            "3"
        ]
    },
    {
        "id": "B",
        "things": [
            "1",
            "1",
            "2",
            "3"
        ]
    },
    {
        "id": "C",
        "things": [
            "1",
            "2",
            "3"
        ]
    }
]

I've tried to use a subquery, but can't seem to get that to work.

3
  • Could you please share what you have tried? or any errors you faced with. Commented Sep 19, 2024 at 17:35
  • Please take the tour, read How to Ask, and then edit to provide a minimal reproducible example. "I've tried to use a subquery" unfortunately doesn't help. Aside from all that: I'm not sure you can accomplish what you want in a single query, as there's no pipeline of aggregations+operations; this might require some iteration through values, whether in a stored procedure or client-side code. Might be beneficial to evolve your schema to store "things" items in separate documents, which will make your grouping and counting more straightforward... Commented Sep 19, 2024 at 19:02
  • I was able to get around this by using a udf. Commented Sep 19, 2024 at 23:22

1 Answer 1

0

I was able to get around this by writing a udf to check for duplicates in the string array.

SELECT 
   c.id,
FROM c 
WHERE
  udf.isDuplicate(c.things) = true
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.