0

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?

wookie
  • 179
  • 2
  • 14
  • What do you mean by copying ```myArray```? I think your looking for $push{ $each: []} https://docs.mongodb.com/manual/reference/operator/update/each/#use-each-with-push-operator – GitGitBoom Aug 18 '20 at 19:31
  • You can't use `push` inside `findAndModify`'s update when you're using the aggregation pipeline. By copying I if mongo loads the stored array to memory in order to concat it with the second array or does it just append the new elements to it – wookie Aug 18 '20 at 19:44

1 Answers1

1

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.

Joe
  • 25,000
  • 3
  • 22
  • 44