0

I'm having a series of documents in cosmos DB where the structure is as follows:

{
    "id" : "s2912vdla"
    "allowed-usrs": [
        {
            "id": "myId"
        }
    ],
    ....
}

I need to be able to get all the documents, which has the id myId in allowed-usrs.id.

Unfortunately, its unclear for me how the SQL Cosmos API would achieve this - I've read in the docs that I should use the IN clause, but I can't seem to get it to work:

SELECT * FROM c IN t.allowed-usrs WHERE c.id = "myId"
2
  • what is the expected outcome? Commented Aug 26, 2021 at 11:17
  • the entire document(s) that contains myId Commented Aug 26, 2021 at 11:18

1 Answer 1

1

Try the following query:

SELECT VALUE c --because we have JOIN we can't use *
FROM c --we need c because we want to return the full object
JOIN a IN c['allowed-usrs'] --brackets required because the char '-' is not allowed
WHERE a.id = 'myId'

or maybe more correctly for your use the query below. The first query will yield duplicate items if myId appears multiple times in the allowed user list (because to the JOIN), while the below only yields one.

SELECT *
FROM c
WHERE ARRAY_CONTAINS(c['allowed-usrs'], { 'id': 'myId' }, true)
--looks for a partial match in the allowed user array
Sign up to request clarification or add additional context in comments.

1 Comment

@JeppeChristensen added a query that's probably better for your context.

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.