Skip to content

Commit 93a9136

Browse files
committed
Fix Mongo adapter ignore mode: pass ignoreDuplicates to client and fix _uid mapping
- Pass ignoreDuplicates option to insertMany for race-condition safety - Extract _uid from raw array before replaceChars (excluded from transformation) - Map inserted records back to original Document objects by _uid
1 parent bee42d4 commit 93a9136

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

src/Database/Adapter/Mongo.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,18 +1576,37 @@ public function createDocuments(Document $collection, array $documents, bool $ig
15761576
}
15771577
}
15781578

1579+
if ($ignore) {
1580+
$options['ignoreDuplicates'] = true;
1581+
}
1582+
15791583
try {
1580-
$documents = $this->client->insertMany($name, $records, $options);
1584+
$inserted = $this->client->insertMany($name, $records, $options);
15811585
} catch (MongoException $e) {
15821586
throw $this->processException($e);
15831587
}
15841588

1585-
foreach ($documents as $index => $document) {
1586-
$documents[$index] = $this->replaceChars('_', '$', $this->client->toArray($document));
1587-
$documents[$index] = new Document($documents[$index]);
1589+
$insertedUids = [];
1590+
foreach ($inserted as $record) {
1591+
$arr = $this->client->toArray($record);
1592+
// _uid is excluded from replaceChars transformation, so extract it before conversion
1593+
$uid = $arr['_uid'] ?? '';
1594+
$arr = $this->replaceChars('_', '$', $arr);
1595+
$insertedUids[$uid] = new Document($arr);
15881596
}
15891597

1590-
return $documents;
1598+
if ($ignore) {
1599+
$result = [];
1600+
foreach ($records as $i => $record) {
1601+
$uid = $record['_uid'] ?? '';
1602+
if (isset($insertedUids[$uid])) {
1603+
$result[] = $documents[$i];
1604+
}
1605+
}
1606+
return $result;
1607+
}
1608+
1609+
return \array_values($insertedUids);
15911610
}
15921611

15931612
/**

0 commit comments

Comments
 (0)