0

I have cosmos db document structure as

{
        "studentId": "lkjsdvoijwvsd",
        "studentFirstName": "FirstName",
        "studentLastName": "lastanme",
        "email": "abc.xyz@example.com",
        "phone": "(111) 444-5555",
        "registeredCourses": [
            "courseId1",
            "courseId2",
            "courseId3",
            "courseId4"
        ]
}

I have been provided with an input array : ["courseId2","courseId5"]. I need to fetch all of the documents which have registeredCourses in input array.

For the above input array I need to fetch all the document which has at least one of the element from input array in registeredCourses

I am trying to get result from :

select *
from c
where (c.registeredCourses INTERSECT ["courseId1, "courseId5"] is not blank )
David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • is it possible to do by cosmos sql query ? Please help – Ainuddin Khan Apr 19 '22 at 07:50
  • you can use this query to get the desired output `Select * from Student s where ARRAY_CONTAINS(s.registeredCourses,"courseId2")` you can refer this doc for your reference https://novacontext.com/writing-and-running-queries-on-nosql-document-databases/ – Pratik Lad Apr 19 '22 at 10:06
  • Please edit your question to show what you've tried, and the issues you're having. Posting a requirement, followed by "provide the exact sql query" isn't really how this site works. Aside from that, this is a duplicate of other questions asking about array inclusion. – David Makogon Apr 19 '22 at 12:22
  • Does this answer your question? [Azure COSMOS DB how to query for contains in an array](https://stackoverflow.com/questions/50662161/azure-cosmos-db-how-to-query-for-contains-in-an-array) – David Makogon Apr 19 '22 at 12:37
  • I am not able to use ARRAY_CONTAINS(argument1, argument2) because in my case agument2 is also array @PratikLad-MT – Ainuddin Khan Apr 19 '22 at 18:32
  • I am not able to use ARRAY_CONTAINS(argument1, argument2) because in my case agument2 is also array @DavidMakogon I am trying to get result from : select * from c where (c.registeredCourses INTERSECT ["courseId1, "courseId5"] is not blank ) – Ainuddin Khan Apr 19 '22 at 18:35

0 Answers0