From e66900c0d3f75ac14130cbd350f08289b112e9f1 Mon Sep 17 00:00:00 2001 From: Joseph Phillips Date: Fri, 8 Nov 2024 10:59:12 +0100 Subject: [PATCH] fix: use $project to minimise size of mongo changestream processing We have to-date been processing entire documents in the Mongo aggregate pipeline the feeds the txnwatcher. This is wasteful, because we only use the ID and revision number. Here we add a $project stanza to the aggregate command, which limits the fields pulled from the collection. --- state/watcher/txnwatcher.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/state/watcher/txnwatcher.go b/state/watcher/txnwatcher.go index 275caecaeba..a97ea9908d8 100644 --- a/state/watcher/txnwatcher.go +++ b/state/watcher/txnwatcher.go @@ -402,6 +402,15 @@ func (w *TxnWatcher) init() (bool, error) { {"pipeline", []bson.D{ {{"$changeStream", cs}}, {{"$match", match}}, + {{"$project", bson.M{ + "operationType": 1, + "documentKey": 1, + "ns": 1, + "updateDescription": 1, + "txnNumber": 1, + "fullDocument._id": 1, + "fullDocument.txn-revno": 1, + }}}, }}, {"cursor", bson.D{{"batchSize", 10}}}, {"readConcern", bson.D{{"level", "majority"}}},