我需要了解Google数据存储中的实体数量。但我无法在他们的api中找到。有没有人知道使用php api获取计数的方法或黑客?
use Google\Cloud\Datastore\DatastoreClient;
$datastore = new DatastoreClient();
$query = $datastore->query();
$query->kind('Companies');
$query->filter('companyName', '=', 'Google');
$res = $datastore->runQuery($query);
$count = 0;
foreach ($res as $company) {
$count++;
}
echo $count;
答案 0 :(得分:1)
查询语言中没有本机计数功能,因为它不是服务器的缩放操作。您可以通过客户端库模拟,但方式与上面的代码非常相似。如果您将其更改为“仅限密钥”,则会更快且成本更低:
$datastore = new DatastoreClient();
$query = $datastore->query();
$query->kind('Companies');
$query->filter('companyName', '=', 'Google');
$query->keysOnly();
$res = $datastore->runQuery($query);
$count = 0;
foreach ($res as $company) {
$count++;
}
echo $count;