$$NOW unavailable after $unionWith in aggregation pipeline

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 8.3.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Execution
    • Fully Compatible
    • ALL
    • v8.2, v8.0
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      While implementing Django ORM compatibility, I found that `$$NOW` becomes unavailable after introducing a $unionWith stage. This appears inconsistent with other pipeline stages, since `$$NOW` works normally if it is referenced before `$unionWith` or stored using `$set`.

       

      Steps to Reproduce

      Pipeline that fails:

      [
        { $match: { num_awards: { $gt: 3 } } },
        { $group: { _id: null, total: { $sum: "$duration" } } },
        { $unionWith: { pipeline: [ { $documents: [ {} ] } ] } },
        { $project: { value: { $ifNull: ["$total", { $subtract: ["$$NOW", "$$NOW"] }] } } }
      ]
      pymongo.errors.OperationFailure: PlanExecutor error during aggregation :: caused by :: Builtin variable '$$NOW' is not available, full error: {'ok': 0.0, 'errmsg': "PlanExecutor error during aggregation :: caused by :: Builtin variable '$$NOW' is not available", 'code': 51144, 'codeName': 'Location51144', '$clusterTime': {'clusterTime': Timestamp(1763611140, 562), 'signature': {'hash': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'keyId': 0}}, 'operationTime': Timestamp(1763611140, 562)}

      Pipeline that works (when storing `$$NOW` beforehand):

      [
        { $set: { __now: "$$NOW" } },
        { $match: { num_awards: { $gt: 3 } } },
        { $group: { _id: null, total: { $sum: "$duration" } } },
        { $unionWith: { pipeline: [ { $documents: [ {} ] } ] } },
        { $project: { value: { $ifNull: ["$total", { $subtract: ["$$NOW", "$$NOW"] }] } } }
      ] 

      MongoDB Version
      8.0.11

            Assignee:
            Ivan Fefer
            Reporter:
            Emanuel Lupi
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: