mongodbfindandmodify

MongoDB $concatArrays in findAndModify


I'm wondering if $concatArrays in the update section of findAndModify will copy the source array, add the new elements to it and save the result to the target array or will it just append the new elements to the existing array without the mentioned copy.

To illustrate it better, here is an example:

db.myCollection.findAndModify ( { _id: 2 },
  [ { $set: { myArray: { $concatArrays: [ "$myArray", [ 1, 2 ]  ] } } } ]
)

Is Mongo smart enough o simply append [1, 2] to myArray without copying myArray in the first place?


Solution

  • The type of operation being performed is not relevant.

    BSON objects are stored in a packed format, so changing the size of the data will certainly require obtaining a slightly larger buffer, copying the original to the new location, and applying the changes.

    The underlying storage engine stores the original document in its cache in an immutable format, along with a skip list of changes that have been made. The skip list is reconciled with the original document and stored on disk during checkpoint or eviction.