KiiObject のカウント
Bucket 内の全 KiiObject 数は count()
メソッドで取得できます。同様に、ある検索条件にヒットする KiiObject の個数は countWithQuery()
メソッドで取得できます。
KiiObject 数は、実行したユーザーから実際にアクセスできる件数をカウントします。ACL によってアクセスできない KiiObject はカウント対象になりません。
KiiObject 数が多い場合、パフォーマンスが低下する場合があります。対処方法は パフォーマンス をご覧ください。
Bucket 内の全 KiiObject を数える
Bucket 内の全 KiiObject 数をカウントする例を以下に挙げます。
-
// 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. } );
-
// 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. } });
検索条件にヒットする KiiObject を数える
検索条件にヒットする KiiObject 数をカウントする例を以下に挙げます。この例では "people" という名前の Bucket 内の KiiObject のうち、"age" が 25 以上のものをカウントしています。
-
// 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. } );
-
// 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. } });