Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo aggregation framework: what is the lock level of the last stage $out operation?

Using Mongo's aggregation pipeline it is possible to write query result to a collection (existent or new) using $out stage, like that

db.my_collection.aggregate([ { $match: { my_field: 'my_value' } }, { $out: 'my_new_collection' } ])

The question is what kind of lock does Mongo use while writing to my_new_collection? Is it a 'regular' write lock, or a global lock, like Map Reduce?

Map Reduce lock reference

like image 784
szymek Avatar asked Aug 05 '15 14:08

szymek


1 Answers

There is always a certain level of locking that depending on your MongoDB version is either likely to be collection or in older the database level, or even possibly document level with the WiredTiger storage engine. The $out does however yield on writes, so indvidual documents are output from the pipeline and not all in one go, so each update is atomic per document.

Even the mapReduce command has this option, where you can set "nonAtomic" as a condition where the output collection of a mapReduce will exhibit the same behavior.

The one thing to be aware of with $out will remove all documents ( not replace any existing indexes ) from a collection as that stage executes when using the "replace" mode. So attempting to read or write from a collection directed with "replace" set is very likely to to fail (or produce unexpected results) whilst the aggregation operation is in progress.

The other limitations relating to sharded collections and capped collections are noted in the documentation.

like image 125
Blakes Seven Avatar answered Sep 28 '22 06:09

Blakes Seven