简体   繁体   中英

mongodb aggregation on updating collections

How does the aggregation cursor react to CRUD(remove R) operations in the used collections ? For example:

db.collection('aggregate')
.aggregate([
      {$match: {}},
      {$project:
        { newField: {$literal: "new value"} }
      }
]).each(function(err, doc) {      
  // do editin inserting and removing on 'aggregate' collection    
 print(doc)
});

Is there a chance that the algorithm will print records added or changed during its operation?

Is there a chance that the algorithm will print records added or changed during its operation?

No. When an Aggregate Operation is performed, there would be an Intent Shared (IS) lock applied on the collection, which means only read operations could happen concurrently. Any Create, Update or Delete operations have to wait for the lock to be removed, because update operations require an Exclusive (X) lock applied.

References :

  1. MongoDB Locking Types - https://docs.mongodb.com/manual/faq/concurrency/#what-type-of-locking-does-mongodb-use .

  2. MongoDB Tutorial - MongoDB Locks Examples

Aggregation operations process data records and return computed results. Cursor is applied on the computed results. Any changes to the collection after aggregation does not affect the already computed results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM