Counting KiiObjects in a Bucket

For counting all KiiObjects in a bucket, use the count() method. For counting the number of KiiObjects that satisfy a specific query condition, use the countWithQuery() method.

These methods count KiiObjects that are accessible to the current user. Those that are not accessible because of the ACL setting are not counted.

The performance of the count operation can deteriorate if there are numerous KiiObjects. See Performance for more information.

Counting all KiiObjects in a bucket

You can get the number of KiiObjects in a bucket with the following sample code:

  • // Prepare the target bucket to be queried.
    var bucket = Kii.bucketWithName("people");
    
    // Count the number of the KiiObjects in the bucket.
    bucket.count({
      success: function(theBucket, theQuery, count) {
        console.log("Number of objects : " + count);
        // The theQuery argument has a query that returns all KiiObjects in the bucket.
      },
      failure: function(theBucket, errorString) {
        // Handle the error.
      }
    });
  • // Prepare the target bucket to be queried.
    var bucket = Kii.bucketWithName("people");
    
    // Count the number of the KiiObjects in the bucket.
    bucket.count().then(
      function(params) {
        var theBucket = params[0];
        var theQuery = params[1];
        var count = params[2];
        console.log("Number of objects : " + count);
        // The theQuery argument has a query that returns all KiiObjects in the bucket.
      }
    ).catch(
      function(error) {
        var theBucket = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );

Counting KiiObjects in a bucket with a query condition

You can get the number of KiiObjects that satisfy a query condition in a bucket with the following sample code. Suppose you have a bucket named "people" and want to know the number of KiiObjects that have a value of 25 or higher for the "age" field.

  • // Prepare the target bucket to be queried.
    var bucket = Kii.bucketWithName("people");
    
    // Create a query that returns KiiObjects whose "age" is equal or greater than 25.
    var clause = KiiClause.greaterThanOrEqual("age", 25);
    var query = KiiQuery.queryWithClause(clause);
    
    // Count the number of the KiiObjects that meet the above condition.
    bucket.countWithQuery(query, {
      success: function(theBucket, theQuery, count) {
        console.log("Number of objects : " + count);
      },
      failure: function(theBucket, errorString) {
        // Handle the error.
      }
    });
  • // Prepare the target bucket to be queried.
    var bucket = Kii.bucketWithName("people");
    
    // Create a query that returns KiiObjects whose "age" is equal or greater than 25.
    var clause = KiiClause.greaterThanOrEqual("age", 25);
    var query = KiiQuery.queryWithClause(clause);
    
    // Count the number of the KiiObjects that meet the above condition.
    bucket.countWithQuery(query).then(
      function(params) {
        var theBucket = params[0];
        var theQuery = params[1];
        var count = params[2];
        console.log("Number of objects : " + count);
      }
    ).catch(
      function(error) {
        var theBucket = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );