Mongol aggregation structure: what is the blocking level of the last $ out operation?

Using the Mongo aggregation pipeline, you can write the result of a query to a collection (existing or new) using the $out step, for example

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

The question is, what kind of lock my_new_collection Mongo use when writing to my_new_collection ? Is this a β€œregular” write lock or a global lock like Map Reduce?

Map Decrease Lock Link

+5
source share
1 answer

There is always a certain level of blocking, which, depending on your version of MongoDB, can be either collection, or an older database level, or even, possibly, a document level with the WiredTiger engine. $out , however, works on write, so individual documents are output from the pipeline, not all at a time, so each update is atomic to the document.

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

The only thing you need to know with $out is to remove all documents from the collection (not replacing existing indexes), since this step is performed using the "replace" mode. Therefore, an attempt to read or write from a collection directed with the "replace" set is likely to lead to failure (or unexpected results) while the aggregation operation is in progress.

Other restrictions related to stunned collections and limited collections are noted in the documentation.

+4
source

All Articles