Firestore PHP - 从文档中获取一个字段

问题描述

所以我有一个像这样的firestore

enter image description here

并且我希望能够从文档中仅获取一个值,例如从突出显示的文档中获取“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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...