-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
Query Integration
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Background
When MongoDB receives an insert, it needs to decide whether to perform a regular insert or a timeseries insert. This decision currently happens independently in two places with duplicated logic.
Current code structure
Path 1: Direct inserts via CmdInsert
When a client sends an insert command, CmdInsert::typedRun() calls getCollectionPreConditionsAndIsTimeseriesLogicalRequest() to check if the target collection is timeseries. Based on the result it calls either performTimeseriesWrites() or performInserts().
Path 2: $out aggregation stage via MongoProcessInterface
The $out stage writes documents through MongoProcessInterface, which exposes two separate virtual methods: insert() and insertTimeseries(). The $out stage determines whether the output collection is timeseries during initialization, stores this in a _timeseries flag, and at flush time branches to call one or the other.
On the implementation side, NonShardServerProcessInterface::insertTimeseries() internally calls getCollectionPreConditionsAndIsTimeseriesLogicalRequest() and then performTimeseriesWrites() — the same logic that CmdInsert does. The sharded implementation ShardServerProcessInterface::insertTimeseries() simply delegates to insert(), since the timeseries dispatch happens on the receiving shard.
The problem
The timeseries-vs-regular branching logic is duplicated: once in CmdInsert::typedRun() and once split across NonShardServerProcessInterface::insert() / insertTimeseries(), with $out also carrying its own detection logic.
Proposal
- Create a unified write function (e.g. in write_ops_exec) that encapsulates the timeseries branching: call getCollectionPreConditionsAndIsTimeseriesLogicalRequest(), then dispatch to performTimeseriesWrites() or performInserts() accordingly.
- CmdInsert::typedRun() calls this unified function instead of branching manually.
- Remove the insertTimeseries() virtual method from MongoProcessInterface entirely. The single insert() method on NonShardServerProcessInterface calls the same unified function. ShardServerProcessInterface is unchanged (it already delegates timeseries to regular insert).
- Simplify $out — stop tracking _timeseries state for the write path. Always set the collection UUID on the insert command. Just call insert() and let the unified function handle timeseries detection.