Skip to content

Commit

Permalink
Fix Collection::upsert() inconsistency w/ update() & delete()
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Dec 30, 2023
1 parent cf5230c commit c1ff59b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ MongoDB for XP Framework ChangeLog

## ?.?.? / ????-??-??

* Fixed `upsert()` inconsistency with `update()` and `delete()` in how
it handles the *query* parameter
(@thekid)

## 2.1.0 / 2023-09-10

* Merged PR #43: Retry "Exception: not primary" when writing (fixes #42)
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/com/mongodb/Collection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function upsert($query, $arg, Options... $options): Update {
$result= $this->proto->write($options, [
'update' => $this->name,
'updates' => [[
'q' => $query instanceof ObjectId ? ['_id' => $query] : $query,
'q' => is_array($query) ? $query : ['_id' => $query],
'u' => $arg,
'upsert' => true,
'multi' => false
Expand Down
6 changes: 3 additions & 3 deletions src/test/php/com/mongodb/unittest/CollectionTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public function insert_many() {
Assert::equals([2, ['one', 'two']], [$result->inserted(), $result->ids()]);
}

#[Test]
public function upsert_when_updating() {
#[Test, Values([[['_id' => 'one']], ['one']])]
public function upsert_when_updating($query) {
$result= $this->newFixture($this->ok(['n' => 1, 'nModified' => 1]))->upsert(
['_id' => 'one'],
$query,
new Document(['_id' => 'one', 'name' => 'A'])
);

Expand Down

0 comments on commit c1ff59b

Please sign in to comment.