简体   繁体   中英

Google Firestore - Access keys/values inside of an array/map in query

I'm currently trying to access the information within a document here. I don't actually know what data structure that is. At first I thought it was an array, now a map, but maybe even key/value pairs?

在此处输入图片说明

The code I use to initalise it is:

$data = [
    'additional_email_info' => 'true',
    'address' => $addressData,
      ...
     ]

Where address is declared in the same way just before it. My current code to try to access the 'city' field:

$docRef = $db->collection('activities');
$query = $docRef->where('address', 'in', [$city_search]);
$documents = $query->documents();

I managed to retrieve information outside of address in the other fields, but I'm not sure how to access this field within the field and there is little documentation on it on their page here https://cloud.google.com/firestore/docs/query-data/queries .

If a field in a Firestore document has subfields, the value you get back when accessing that field is a dictionary/array. So you can access the city of a your example document with:

$docRef = $db->collection('activities');
$query = $docRef->where('address', 'in', [$city_search]);
$documents = $query->documents();

foreach ($documents as $city) {
    printf('Doc : %s' . PHP_EOL, $city->id());
    printf('Date: %s' . PHP_EOL, $city['date']);
    printf('City: %s' . PHP_EOL, $city['address']['city']);
    printf(PHP_EOL);
}

If you want to query on the city, you'd use dot notation :

$docRef->where('address.city', '=', 'London');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM