On Array Value

Queries on array values.

You can run queries on array value.

Examples

Retrieve all objects with an array that contains the number 1:
curl -X POST \
  -H "X-Appery-Database-Id: 544a5cdfe4b03d005b6233b9" \
  -H "Content-Type: application/json" \
  -d '{"where": { "marks": 1}}' \
  https://api.appery.io/rest/1/db/collections/students/query

📘

Note

There are several special operators that MongoDB provides for array queries.

The $all operator can be used to select the documents where the value of a field is an array that contains all the specified elements.

Retrieve all objects that contain array with 5, 6 and 7:

curl -X POST \
  -H "X-Appery-Database-Id: 544a5cdfe4b03d005b6233b9" \
  -H "Content-Type: application/json" \
  -d '{"where": {"marks": {"$all": [5, 7, 6 ]}}}' \
  https://api.appery.io/rest/1/db/collections/students/query
It’s possible to retrieve elements by array size, using the $size operator.

Retrieve all the objects that contain an array with 3 elements:

curl -X POST \
  -H "X-Appery-Database-Id: 544a5cdfe4b03d005b6233b9" \
  -H "Content-Type: application/json" \
  -d '{"where": {"marks": {"$size": 3}}}' \
  https://api.appery.io/rest/1/db/collections/students/query
It’s also possible to use the $elemMatch operator to match more than one component within an array element. In this case, the array structure must be a little more complex.

The marks column with array of objects:

studentName

studentId

marks

string

number

array

John

10

[{“math”:”5″, “physics”:”7″}]

By using the $elemMatch operator, it’s possible to make a query for each of object fields. Therefore, it’s possible to retrieve all object arrays where the mark for math equals 5, and mark for physics is greater than 6:

curl -X POST \
  -H "X-Appery-Database-Id: 544a5cdfe4b03d005b6233b9" \
  -H "Content-Type: application/json" \
  -d '{"where": {"marks": {"$elemMatch": {"math": "5","physics" : {"$gt": "6"}}}}}' \
  https://api.appery.io/rest/1/db/collections/students/query