COUNT (NoSQL query)

APPLIES TO: NoSQL

Returns the count of the values in the expression.

Syntax

COUNT(<scalar_expr>)  

Arguments

Description
scalar_expr A scalar expression.

Return types

Returns a numeric scalar value.

Examples

This first example passes in either a scalar value or a numeric expression to the COUNT function. The expression is evaluated first to a scalar, making the result of both uses of the function the same value.

SELECT VALUE {
    countScalar: COUNT(1),
    countExpression: COUNT(2 + 3)
}
[
  {
    "countScalar": 1,
    "countExpression": 1
  }
]

This next example assumes that there's a container with two items with a /name field. There's one item without the same field.

[
  {
    "name": "Horric socks",
    "category": "socks"
  },
  {
    "name": "Shinity socks",
    "category": "socks"
  },
  {
    "category": "socks"
  }
]

In this example, the function counts the number of times the specified scalar field occurs in the filtered data. Here, the function looks for the number of times the /name field occurs which is two out of three times.

SELECT VALUE
    COUNT(p.name)
FROM
    products p
WHERE
    p.category = "socks"
[
  2
]

In this final example, the function is used to count every item within a contianer.

SELECT VALUE
    COUNT(1)
FROM
    items

Remarks

  • This function benefits from a range index for any properties in the query's filter.