@@ -616,7 +616,7 @@ public function createCollection(string $name, array $attributes = [], array $in
616616 $ partialFilter = [];
617617 foreach ($ attributes as $ attr ) {
618618 // Find the matching attribute in collectionAttributes to get its type
619- $ attrType = ' string ' ; // Default fallback
619+ $ attrType = $ this -> getMongoTypeCode ( null );
620620 foreach ($ collectionAttributes as $ collectionAttr ) {
621621 if ($ collectionAttr ->getId () === $ attr ) {
622622 $ attrType = $ this ->getMongoTypeCode ($ collectionAttr ->getAttribute ('type ' ));
@@ -1027,6 +1027,9 @@ public function createIndex(string $collection, string $id, string $type, array
10271027 $ indexes ['key ' ]['_tenant ' ] = $ this ->getOrder (Database::ORDER_ASC );
10281028 }
10291029
1030+ // Types are keyed by attribute name, which the loop below replaces with the internal key
1031+ $ bsonTypes = \array_map (fn (string $ attribute ) => $ this ->getMongoTypeCode ($ indexAttributeTypes [$ attribute ] ?? null ), $ attributes );
1032+
10301033 foreach ($ attributes as $ i => $ attribute ) {
10311034
10321035 if (isset ($ indexAttributeTypes [$ attribute ]) && \str_contains ($ attribute , '. ' ) && $ indexAttributeTypes [$ attribute ] === Database::VAR_OBJECT ) {
@@ -1088,9 +1091,7 @@ public function createIndex(string $collection, string $id, string $type, array
10881091 if (in_array ($ type , [Database::INDEX_UNIQUE , Database::INDEX_KEY ])) {
10891092 $ partialFilter = [];
10901093 foreach ($ attributes as $ i => $ attr ) {
1091- $ attrType = $ indexAttributeTypes [$ i ] ?? Database::VAR_STRING ; // Default to string if type not provided
1092- $ attrType = $ this ->getMongoTypeCode ($ attrType );
1093- $ partialFilter [$ attr ] = ['$exists ' => true , '$type ' => $ attrType ];
1094+ $ partialFilter [$ attr ] = ['$exists ' => true , '$type ' => $ bsonTypes [$ i ]];
10941095 }
10951096 if (!empty ($ partialFilter )) {
10961097 $ indexes ['partialFilterExpression ' ] = $ partialFilter ;
@@ -1146,6 +1147,11 @@ public function createIndex(string $collection, string $id, string $type, array
11461147
11471148 return $ result ;
11481149 } catch (\Exception $ e ) {
1150+ // Existing documents violate the new unique index, whatever index the message names
1151+ if ($ e ->getCode () === 11000 || $ e ->getCode () === 11001 ) {
1152+ throw new UniqueException ('Unique index violation ' , $ e ->getCode (), $ e );
1153+ }
1154+
11491155 throw $ this ->processException ($ e );
11501156 }
11511157 }
@@ -2670,24 +2676,30 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
26702676 /**
26712677 * Converts Appwrite database type to MongoDB BSON type code.
26722678 *
2673- * @param string $appwriteType
2674- * @return string
2679+ * Numbers use the 'number' alias: an integer is stored as int32 or int64
2680+ * depending on its value, and a float attribute can hold an integer.
2681+ * An unknown type (schemaless, internal attributes) matches every stored
2682+ * value type except null.
2683+ *
2684+ * @param string|null $appwriteType
2685+ * @return string|array<string>
26752686 */
2676- private function getMongoTypeCode (string $ appwriteType ): string
2687+ private function getMongoTypeCode (? string $ appwriteType ): string | array
26772688 {
26782689 return match ($ appwriteType ) {
26792690 Database::VAR_STRING => 'string ' ,
26802691 Database::VAR_VARCHAR => 'string ' ,
26812692 Database::VAR_TEXT => 'string ' ,
26822693 Database::VAR_MEDIUMTEXT => 'string ' ,
26832694 Database::VAR_LONGTEXT => 'string ' ,
2684- Database::VAR_INTEGER => 'int ' ,
2685- Database::VAR_BIGINT => 'long ' ,
2686- Database::VAR_FLOAT => 'double ' ,
2695+ Database::VAR_INTEGER => 'number ' ,
2696+ Database::VAR_BIGINT => 'number ' ,
2697+ Database::VAR_FLOAT => 'number ' ,
26872698 Database::VAR_BOOLEAN => 'bool ' ,
26882699 Database::VAR_DATETIME => 'date ' ,
26892700 Database::VAR_ID => 'string ' ,
26902701 Database::VAR_UUID7 => 'string ' ,
2702+ null => ['string ' , 'number ' , 'bool ' , 'date ' , 'object ' ],
26912703 default => 'string '
26922704 };
26932705 }
0 commit comments