Skip to content

Commit 5a88478

Browse files
authored
chore: update firestore ineq sample to use cities (GoogleCloudPlatform#2036)
1 parent ce07a14 commit 5a88478

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

firestore/src/data_get_dataset.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,41 @@ function data_get_dataset(string $projectId): void
4343
'state' => 'CA',
4444
'country' => 'USA',
4545
'capital' => false,
46-
'population' => 860000
46+
'population' => 860000,
47+
'density' => 18000,
4748
]);
4849
$citiesRef->document('LA')->set([
4950
'name' => 'Los Angeles',
5051
'state' => 'CA',
5152
'country' => 'USA',
5253
'capital' => false,
53-
'population' => 3900000
54+
'population' => 3900000,
55+
'density' => 8000,
5456
]);
5557
$citiesRef->document('DC')->set([
5658
'name' => 'Washington D.C.',
5759
'state' => null,
5860
'country' => 'USA',
5961
'capital' => true,
60-
'population' => 680000
62+
'population' => 680000,
63+
'density' => 11000,
6164
]);
6265
$citiesRef->document('TOK')->set([
6366
'name' => 'Tokyo',
6467
'state' => null,
6568
'country' => 'Japan',
6669
'capital' => true,
67-
'population' => 9000000
70+
'population' => 9000000,
71+
'density' => 16000,
72+
6873
]);
6974
$citiesRef->document('BJ')->set([
7075
'name' => 'Beijing',
7176
'state' => null,
7277
'country' => 'China',
7378
'capital' => true,
74-
'population' => 21500000
79+
'population' => 21500000,
80+
'density' => 3500,
7581
]);
7682
printf('Added example cities data to the cities collection.' . PHP_EOL);
7783
# [END firestore_data_get_dataset]

firestore/src/query_filter_compound_multi_ineq.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,17 @@ function query_filter_compound_multi_ineq(string $projectId): void
3737
$db = new FirestoreClient([
3838
'projectId' => $projectId,
3939
]);
40-
$collection = $db->collection('samples/php/users');
41-
// Setup the data before querying for it
42-
$collection->document('person1')->set(['age' => 23, 'height' => 65]);
43-
$collection->document('person2')->set(['age' => 37, 'height' => 55]);
44-
$collection->document('person3')->set(['age' => 40, 'height' => 75]);
45-
$collection->document('person4')->set(['age' => 40, 'height' => 65]);
40+
$collection = $db->collection('samples/php/cities');
4641

4742
# [START firestore_query_filter_compound_multi_ineq]
4843
$chainedQuery = $collection
49-
->where('age', '>', 35)
50-
->where('height', '>', 60)
51-
->where('height', '<', 70);
44+
->where('population', '>', 1000000)
45+
->where('density', '<', 10000);
46+
5247
# [END firestore_query_filter_compound_multi_ineq]
5348
foreach ($chainedQuery->documents() as $document) {
5449
printf(
55-
'Document %s returned by age > 35 and height between 60 and 70' . PHP_EOL,
50+
'Document %s returned by population > 1000000 and density < 10000' . PHP_EOL,
5651
$document->id()
5752
);
5853
}

firestore/src/query_filter_dataset.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function query_filter_dataset(string $projectId): void
4444
'country' => 'USA',
4545
'capital' => false,
4646
'population' => 860000,
47+
'density' => 18000,
4748
'regions' => ['west_coast', 'norcal']
4849
]);
4950
$citiesRef->document('LA')->set([
@@ -52,6 +53,7 @@ function query_filter_dataset(string $projectId): void
5253
'country' => 'USA',
5354
'capital' => false,
5455
'population' => 3900000,
56+
'density' => 8000,
5557
'regions' => ['west_coast', 'socal']
5658
]);
5759
$citiesRef->document('DC')->set([
@@ -60,6 +62,7 @@ function query_filter_dataset(string $projectId): void
6062
'country' => 'USA',
6163
'capital' => true,
6264
'population' => 680000,
65+
'density' => 11000,
6366
'regions' => ['east_coast']
6467
]);
6568
$citiesRef->document('TOK')->set([
@@ -68,6 +71,7 @@ function query_filter_dataset(string $projectId): void
6871
'country' => 'Japan',
6972
'capital' => true,
7073
'population' => 9000000,
74+
'density' => 16000,
7175
'regions' => ['kanto', 'honshu']
7276
]);
7377
$citiesRef->document('BJ')->set([
@@ -76,6 +80,7 @@ function query_filter_dataset(string $projectId): void
7680
'country' => 'China',
7781
'capital' => true,
7882
'population' => 21500000,
83+
'density' => 3500,
7984
'regions' => ['jingjinji', 'hebei']
8085
]);
8186
printf('Added example cities data to the cities collection.' . PHP_EOL);

firestore/test/firestoreTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ public function testChainedQuery()
277277
public function testChainedInequalityQuery()
278278
{
279279
$output = $this->runFirestoreSnippet('query_filter_compound_multi_ineq');
280-
$this->assertStringContainsString('Document person4 returned by age > 35 and height between 60 and 70', $output);
280+
$this->assertStringContainsString('Document LA returned by population > 1000000 and density < 10000', $output);
281+
$this->assertStringContainsString('Document BJ returned by population > 1000000 and density < 10000', $output);
281282
}
282283

283284
/**

0 commit comments

Comments
 (0)