$tsIncrement

Extracts the increment portion from a timestamp value, returning the increment component of a MongoDB timestamp which consists of time and increment values.

Syntax

{ $tsIncrement: <expression> }

Parameters

expressionobjectrequired

Expression that evaluates to a timestamp, returns error if not a timestamp

Examples

Extract increment from audit timestamp

Get the increment value from last updated timestamp in audit log

Query:

db.stores.aggregate([{ $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} }, { $project: { name: 1, lastUpdatedIncrement: { $tsIncrement: "$auditLog.lastUpdated" } } }])

Output:

Document with lastUpdatedIncrement field containing the timestamp increment value

Extract increment for timestamp comparison

Compare increment values between different timestamps

Query:

db.events.aggregate([{ $project: { eventId: 1, startIncrement: { $tsIncrement: "$startTime" }, endIncrement: { $tsIncrement: "$endTime" } } }])

Output:

Documents with separate increment values for start and end timestamps

Related