问题描述
所以我有一个像这样的firestore
并且我希望能够从文档中仅获取一个值,例如从突出显示的文档中获取“uid”,我的 php 代码如下所示:
Address
调用它的代码如下所示:
<?php
require 'vendor/autoload.php';
//putenv('upheld-pursuit-274606-237ac40b2662.json');
use Google\Cloud\Firestore\FirestoreClient;
class Firestore
{
protected $db;
protected $name;
public function __construct(string $collection)
{
$this->db = new FirestoreClient([
'keyFilePath' => 'upheld-pursuit-274606-237ac40b2662.json','projectId' => 'upheld-pursuit-274606'
]);
$this->name = $collection;
}
public function getWhere(string $field,string $operator,$value)
{
$arr = [];
$query = $this->db->collection($this->name)->where($field,$operator,$value)->documents()->rows();
if(!empty($query)){
foreach ($query as $q){
$arr[] = $q->data();
}
}
return $arr;
}
public function get(string $field)
{
$arr = [];
$query = $this->db->collection($this->name)->document($field)();
if(!empty($query)){
/*foreach ($query as $q){
$arr[] = $q->data();
}*/
throw new Exception('Document does not exist!');
}
return $query;
}
}
?>
如果我需要某些 if 语句或其他条件语句以及用于检查目的,我希望能够仅从文档中检索一个字段
解决方法
看来您只能使用适用于 Cloud Firestore 的 NodeJS 包来执行此操作。 在 this answer 中有一个指向 the doc for firestore select 的链接。
,与任何其他客户端库一样,PHP google/cloud-firestore
具有 select method,它允许您选择要检索的字段。
如果您需要有关如何设置这些查询的更多示例,我建议您查看 simple_queries.php
example。