xp-forge / mongodb

MongoDB for the XP Framework
0 stars 0 forks source link

Add Document::merge() #33

Closed thekid closed 1 year ago

thekid commented 1 year ago

This pull request adds a merge() method to the Document class. It modifies list and map properties like PHP's array_merge() function.

use com\mongodb\{Document, ObjectId};

$doc= new Document(['_id' => ObjectId::create(), 'list' => [1, 2, 3]]);

// This does not work - offsetGet() creates a copy of the array!
$doc['list'][]= 4;

// What we need to do
$doc['list']= array_merge($doc['list'] ?? [], [4]);

// New functionality:
$doc->merge('list', [4]);
thekid commented 1 year ago

We should be using $push, $set etcetera instead of writing the entire document