Using Variables on Compass Aggregation tool

Hello, I’m using Mongodb Compass v1.41.0, and would like to know if there is a way to use and define variables on aggregations tab, I created a script for data analysis and uses some values multiple times, sometimes I need to change those values, and is annoying to change these on all stages, is there any way to use variables or any idea that may solve the issue? Thanks in advance

image

1 Like

We could help better if you share your aggregation. But without knowing more I can point you to $let which would be appropriate in some cases.

Another way it to use $addField to set temporary fields that you could use later as any other fields.

oh sorry, here. I would like to create variables for “Entry Date” and “Source” fields

[
  {
    $match:
      /**
       * query: The query in MQL.
       */
      {
        Source: "Clientx",
        "Entry Date": {
          $gte: ISODate(
            "2024-06-01T04:00:00.000+00:00"
          ),
          $lte: ISODate(
            "2024-07-01T04:00:00.000+00:00"
          ),
        },
      },
  },
  {
    $facet:
      /**
       * outputFieldN: The first output field.
       * stageN: The first aggregation stage.
       */
      {
        TotalClicks: [
          {
            $group: {
              _id: "$Source",
              TotalClicks: {
                $sum: 1,
              },
              TotalCost: {
                $sum: "$Cost",
              },
            },
          },
        ],
        UniqueIps: [
          {
            $group: {
              _id: "$IP Address",
            },
          },
          {
            $count: "UniqueIps",
          },
        ],
      },
  },
  {
    $unwind:
      /**
       * path: Path to the array field.
       * includeArrayIndex: Optional name for index.
       * preserveNullAndEmptyArrays: Optional
       *   toggle to unwind null and empty values.
       */
      {
        path: "$TotalClicks",
      },
  },
  {
    $unwind:
      /**
       * path: Path to the array field.
       * includeArrayIndex: Optional name for index.
       * preserveNullAndEmptyArrays: Optional
       *   toggle to unwind null and empty values.
       */
      {
        path: "$UniqueIps",
      },
  },
  {
    $project:
      /**
       * specifications: The fields to
       *   include or exclude.
       */
      {
        Source: "$TotalClicks._id",
        Total_Cost: "$TotalClicks.TotalCost",
        Total_Clicks: "$TotalClicks.TotalClicks",
        Unique_Clicks: "$UniqueIps.UniqueIps",
        Duplicate_Rate: {
          $concat: [
            {
              $toString: {
                $multiply: [
                  {
                    $round: [
                      {
                        $divide: [
                          {
                            $subtract: [
                              "$TotalClicks.TotalClicks",
                              "$UniqueIps.UniqueIps",
                            ],
                          },
                          "$TotalClicks.TotalClicks",
                        ],
                      },
                      4,
                    ],
                  },
                  100,
                ],
              },
            },
            "%",
          ],
        },
      },
  },
]

I like to use the aggregation interface because I can save pipelines there

Did you get any idea of how can we use variables in pipelines so that i can re-use them in other pipelines, something similar to postman in fact .

Hey there, as far as I know, you can’t call variables in Compass or Atlas, you could use python with pymongo to use variables