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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With