diff --git a/Firestore/src/Query.php b/Firestore/src/Query.php index c743705b4f2..ccbd7cd3d66 100644 --- a/Firestore/src/Query.php +++ b/Firestore/src/Query.php @@ -101,6 +101,7 @@ class Query 'array-contains' => FieldFilterOperator::ARRAY_CONTAINS, 'array-contains-any' => FieldFilterOperator::ARRAY_CONTAINS_ANY, 'in' => FieldFilterOperator::IN, + 'not_in' => FieldFilterOperator::NOT_IN, ]; private $allowedDirections = [ @@ -1024,7 +1025,7 @@ private function createFieldFilter($fieldPath, $operator, $value) } if ($escapedPathString === self::DOCUMENT_ID) { - if ($operator === FieldFilterOperator::IN) { + if ($operator === FieldFilterOperator::IN || $operator === FieldFilterOperator::NOT_IN) { $value = array_map(function ($value) use ($basePath) { return $this->createDocumentReference($basePath, $value); }, (array) $value); @@ -1051,7 +1052,7 @@ private function createFieldFilter($fieldPath, $operator, $value) ] ]; } else { - $encodedValue = $operator === FieldFilterOperator::IN + $encodedValue = ($operator === FieldFilterOperator::IN || $operator === FieldFilterOperator::NOT_IN) ? $this->valueMapper->encodeMultiValue((array)$value) : $this->valueMapper->encodeValue($value); diff --git a/Firestore/tests/System/QueryTest.php b/Firestore/tests/System/QueryTest.php index 707d3bcddb6..1c761a1d970 100644 --- a/Firestore/tests/System/QueryTest.php +++ b/Firestore/tests/System/QueryTest.php @@ -174,6 +174,77 @@ public function testWhereInArray() $this->assertContains($doc2->id(), $doc_ids); } + public function testWhereNotInArray() + { + $name = $this->query->name(); + $doc1 = $this->insertDoc([ + 'foos' => ['foo', 'bar'], + ]); + $doc2 = $this->insertDoc([ + 'foos' => ['foo'], + ]); + + $docs = self::$client->collection($name)->where('foos', 'not_in', [['foo']])->documents()->rows(); + $this->assertCount(1, $docs); + $this->assertEquals($doc1->name(), $docs[0]->name()); + + $docs = self::$client->collection($name)->where('foos', 'not_in', [['foo'], ['foo', 'bar']]) + ->documents()->rows(); + $this->assertEmpty($docs); + + $docs = self::$client->collection($name)->where('foos', 'not_in', [['foo', 'bar']])->documents()->rows(); + $this->assertCount(1, $docs); + $this->assertEquals($doc2->name(), $docs[0]->name()); + + $docs = self::$client->collection($name) + ->where(FieldPath::documentId(), 'not_in', [$doc1->id(), $doc2->id()]) + ->documents() + ->rows(); + $this->assertEmpty($docs); + + $docs = self::$client->collection($name) + ->where(FieldPath::documentId(), 'not_in', ['non-existent-id']) + ->documents() + ->rows(); + $this->assertCount(2, $docs); + $doc_ids = array_map(function ($doc) { + return $doc->id(); + }, $docs); + $this->assertContains($doc1->id(), $doc_ids); + $this->assertContains($doc2->id(), $doc_ids); + + $docs = self::$client->collection($name) + ->where(Filter::field('foos', 'not_in', [['foo']]))->documents()->rows(); + $this->assertCount(1, $docs); + $this->assertEquals($doc1->name(), $docs[0]->name()); + + $docs = self::$client->collection($name) + ->where(Filter::field('foos', 'not_in', [['foo'], ['foo', 'bar']]))->documents()->rows(); + $this->assertEmpty($docs); + + $docs = self::$client->collection($name) + ->where(Filter::field('foos', 'not_in', [['foo', 'bar']]))->documents()->rows(); + $this->assertCount(1, $docs); + $this->assertEquals($doc2->name(), $docs[0]->name()); + + $docs = self::$client->collection($name) + ->where(Filter::field(FieldPath::documentId(), 'not_in', [$doc1->id(), $doc2->id()])) + ->documents() + ->rows(); + $this->assertEmpty($docs); + + $docs = self::$client->collection($name) + ->where(Filter::field(FieldPath::documentId(), 'not_in', ['non-existent-id'])) + ->documents() + ->rows(); + $this->assertCount(2, $docs); + $doc_ids = array_map(function ($doc) { + return $doc->id(); + }, $docs); + $this->assertContains($doc1->id(), $doc_ids); + $this->assertContains($doc2->id(), $doc_ids); + } + public function testWhereWithCompositeFilter() { $name = $this->query->name(); diff --git a/Firestore/tests/Unit/QueryTest.php b/Firestore/tests/Unit/QueryTest.php index 10e25ce74e6..5fff8ad9dba 100644 --- a/Firestore/tests/Unit/QueryTest.php +++ b/Firestore/tests/Unit/QueryTest.php @@ -448,6 +448,8 @@ public function whereOperators() ['array-contains'], ['in'], ['IN'], + ['not_in'], + ['NOT_IN'], ['array-contains-any'], ]); }