Counting KiiObjects in a Bucket

You can count the number of all KiiObjects in a bucket. You can also count the number of KiiObjects that match with the specified query conditions.

The method counts KiiObjects that are accessible by the current user. The method will not count KiiObjects that are restricted by ACL settings.

The performance of the method can deteriorate if there are numerous KiiObjects. Please read Performance for the related discussion.

Counting all KiiObjects in a bucket

You can get the number of all KiiObjects in a bucket as follows:

curl -v -X GET \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  "https://api.kii.com/api/apps/{APP_ID}/users/me/buckets/{BUCKET_ID}"

Kii Cloud returns the number of KiiObjects as the value of "size".

< HTTP/1.1 200 OK
< Content-Type: application/vnd.kii.BucketRetrievalResponse+json
<
{
  "bucketType" : "rw",
  "size" : {OBJECT_COUNT}
}

Counting KiiObjects in a bucket with query

Suppose you want to know the number of KiiObjects with their field "age" having the values greater than or equal to 25. You can get this count as follows:

curl -v -X POST \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  -H "Content-Type: application/vnd.kii.QueryRequest+json" \
  "https://api.kii.com/api/apps/{APP_ID}/users/me/buckets/{BUCKET_ID}/query" \
   -d '{
    "bucketQuery" : {
      "clause" : {
        "type" : "range",
        "field" : "age",
        "lowerLimit" : 25,
        "lowerIncluded" : true
      },
      "aggregations" : [ {
        "type" : "COUNT",
        "putAggregationInto" : "count_field"
      } ]
    }
  }'

Set the query condition as you would do with Querying KiiObjects.

For getting the count, set the following information in the "aggregations" field:

  • "type": Set "COUNT" for getting the KiiObjects count.
  • "putAggregationInto": Set the name of the field in which you want to get the count.

Kii Cloud returns the count as follows:

< HTTP/1.1 200 OK
< Content-Type: application/vnd.kii.QueryResponse+json
<
{
  "queryDescription" : "SELECT count(*) as count_field WHERE ( 10 < count )",
  "aggregations" : {
    "count_field" : {OBJECT_COUNT}
  }
}