-
Notifications
You must be signed in to change notification settings - Fork 55
Add support for exists and notExists query types in Mongo #776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ class Query | |
| public const TYPE_NOT_STARTS_WITH = 'notStartsWith'; | ||
| public const TYPE_ENDS_WITH = 'endsWith'; | ||
| public const TYPE_NOT_ENDS_WITH = 'notEndsWith'; | ||
| public const TYPE_EXISTS = 'exists'; | ||
| public const TYPE_NOT_EXISTS = 'notExists'; | ||
|
|
||
| // Spatial methods | ||
| public const TYPE_CROSSES = 'crosses'; | ||
|
|
@@ -99,6 +101,8 @@ class Query | |
| self::TYPE_VECTOR_DOT, | ||
| self::TYPE_VECTOR_COSINE, | ||
| self::TYPE_VECTOR_EUCLIDEAN, | ||
| self::TYPE_EXISTS, | ||
| self::TYPE_NOT_EXISTS, | ||
| self::TYPE_SELECT, | ||
| self::TYPE_ORDER_DESC, | ||
| self::TYPE_ORDER_ASC, | ||
|
|
@@ -294,7 +298,9 @@ public static function isMethod(string $value): bool | |
| self::TYPE_SELECT, | ||
| self::TYPE_VECTOR_DOT, | ||
| self::TYPE_VECTOR_COSINE, | ||
| self::TYPE_VECTOR_EUCLIDEAN => true, | ||
| self::TYPE_VECTOR_EUCLIDEAN, | ||
| self::TYPE_EXISTS, | ||
| self::TYPE_NOT_EXISTS => true, | ||
| default => false, | ||
| }; | ||
| } | ||
|
|
@@ -1178,4 +1184,26 @@ public static function vectorEuclidean(string $attribute, array $vector): self | |
| { | ||
| return new self(self::TYPE_VECTOR_EUCLIDEAN, $attribute, [$vector]); | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to create Query with exists method | ||
| * | ||
| * @param string $attribute | ||
| * @return Query | ||
| */ | ||
| public static function exists(string $attribute): self | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's accept an array, and use OR semantics when there are multiple values, same as equal etc. |
||
| { | ||
| return new self(self::TYPE_EXISTS, $attribute); | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to create Query with notExists method | ||
| * | ||
| * @param string $attribute | ||
| * @return Query | ||
| */ | ||
| public static function notExists(string $attribute): self | ||
| { | ||
| return new self(self::TYPE_NOT_EXISTS, $attribute); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1155,4 +1155,131 @@ public function testSchemalessDates(): void | |
|
|
||
| $database->deleteCollection($col); | ||
| } | ||
|
|
||
| public function testSchemalessExists(): void | ||
| { | ||
| /** @var Database $database */ | ||
| $database = static::getDatabase(); | ||
|
|
||
| if ($database->getAdapter()->getSupportForAttributes()) { | ||
| $this->expectNotToPerformAssertions(); | ||
| return; | ||
| } | ||
|
|
||
| $colName = uniqid('schemaless_exists'); | ||
| $database->createCollection($colName); | ||
|
|
||
| $permissions = [ | ||
| Permission::read(Role::any()), | ||
| Permission::write(Role::any()), | ||
| Permission::update(Role::any()), | ||
| Permission::delete(Role::any()) | ||
| ]; | ||
|
|
||
| // Create documents with and without the 'optionalField' attribute | ||
| $docs = [ | ||
| new Document(['$id' => 'doc1', '$permissions' => $permissions, 'optionalField' => 'value1', 'name' => 'doc1']), | ||
| new Document(['$id' => 'doc2', '$permissions' => $permissions, 'optionalField' => 'value2', 'name' => 'doc2']), | ||
| new Document(['$id' => 'doc3', '$permissions' => $permissions, 'name' => 'doc3']), // no optionalField | ||
| new Document(['$id' => 'doc4', '$permissions' => $permissions, 'optionalField' => null, 'name' => 'doc4']), // exists but null | ||
| new Document(['$id' => 'doc5', '$permissions' => $permissions, 'name' => 'doc5']), // no optionalField | ||
| ]; | ||
| $this->assertEquals(5, $database->createDocuments($colName, $docs)); | ||
|
|
||
| // Test exists - should return documents where optionalField exists (even if null) | ||
| $documents = $database->find($colName, [ | ||
| Query::exists('optionalField'), | ||
| ]); | ||
|
|
||
| $this->assertEquals(3, count($documents)); // doc1, doc2, doc4 | ||
| $ids = array_map(fn ($doc) => $doc->getId(), $documents); | ||
| $this->assertContains('doc1', $ids); | ||
| $this->assertContains('doc2', $ids); | ||
| $this->assertContains('doc4', $ids); | ||
|
|
||
| // Verify that doc4 is included even though optionalField is null | ||
| $doc4 = array_filter($documents, fn ($doc) => $doc->getId() === 'doc4'); | ||
| $this->assertCount(1, $doc4); | ||
| $doc4Array = array_values($doc4); | ||
| $this->assertTrue(array_key_exists('optionalField', $doc4Array[0]->getAttributes())); | ||
|
|
||
| // Test exists with another attribute | ||
| $documents = $database->find($colName, [ | ||
| Query::exists('name'), | ||
| ]); | ||
| $this->assertEquals(5, count($documents)); // All documents have 'name' | ||
|
|
||
| // Test exists with non-existent attribute | ||
| $documents = $database->find($colName, [ | ||
| Query::exists('nonExistentField'), | ||
| ]); | ||
| $this->assertEquals(0, count($documents)); | ||
|
|
||
| $database->deleteCollection($colName); | ||
|
Comment on lines
+1212
to
+1273
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once we accept an array in Query::exists, let's add checks for:
|
||
| } | ||
|
|
||
| public function testSchemalessNotExists(): void | ||
| { | ||
| /** @var Database $database */ | ||
| $database = static::getDatabase(); | ||
|
|
||
| if ($database->getAdapter()->getSupportForAttributes()) { | ||
| $this->expectNotToPerformAssertions(); | ||
| return; | ||
| } | ||
|
|
||
| $colName = uniqid('schemaless_not_exists'); | ||
| $database->createCollection($colName); | ||
|
|
||
| $permissions = [ | ||
| Permission::read(Role::any()), | ||
| Permission::write(Role::any()), | ||
| Permission::update(Role::any()), | ||
| Permission::delete(Role::any()) | ||
| ]; | ||
|
|
||
| // Create documents with and without the 'optionalField' attribute | ||
| $docs = [ | ||
| new Document(['$id' => 'doc1', '$permissions' => $permissions, 'optionalField' => 'value1', 'name' => 'doc1']), | ||
| new Document(['$id' => 'doc2', '$permissions' => $permissions, 'optionalField' => 'value2', 'name' => 'doc2']), | ||
| new Document(['$id' => 'doc3', '$permissions' => $permissions, 'name' => 'doc3']), // no optionalField | ||
| new Document(['$id' => 'doc4', '$permissions' => $permissions, 'optionalField' => null, 'name' => 'doc4']), // exists but null | ||
| new Document(['$id' => 'doc5', '$permissions' => $permissions, 'name' => 'doc5']), // no optionalField | ||
| ]; | ||
| $this->assertEquals(5, $database->createDocuments($colName, $docs)); | ||
|
|
||
| // Test notExists - should return documents where optionalField does not exist | ||
| $documents = $database->find($colName, [ | ||
| Query::notExists('optionalField'), | ||
| ]); | ||
|
|
||
| $this->assertEquals(2, count($documents)); // doc3, doc5 | ||
| $ids = array_map(fn ($doc) => $doc->getId(), $documents); | ||
| $this->assertContains('doc3', $ids); | ||
| $this->assertContains('doc5', $ids); | ||
|
|
||
| // Verify that doc4 is NOT included (it exists even though null) | ||
| $this->assertNotContains('doc4', $ids); | ||
|
|
||
| // Test notExists with another attribute | ||
| $documents = $database->find($colName, [ | ||
| Query::notExists('name'), | ||
| ]); | ||
| $this->assertEquals(0, count($documents)); // All documents have 'name' | ||
|
|
||
| // Test notExists with non-existent attribute | ||
| $documents = $database->find($colName, [ | ||
| Query::notExists('nonExistentField'), | ||
| ]); | ||
| $this->assertEquals(5, count($documents)); // All documents don't have this field | ||
|
|
||
| // Test combination of exists and notExists | ||
| $documents = $database->find($colName, [ | ||
| Query::exists('name'), | ||
| Query::notExists('optionalField'), | ||
| ]); | ||
| $this->assertEquals(2, count($documents)); // doc3, doc5 | ||
|
|
||
| $database->deleteCollection($colName); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.