The $divide accumulator is a unary operator

Viewed 2609

I created a request to Mongo DB:

{
    $project:
      {
        difference:
          {
            $subtract: ['$' + endDate, '$' + startDate]
          }
      }
  },
  {
    $match:
      {
        difference:
          {
            $gte: 0
          }
      }
  },
  {
    $group:
      {
        _id: null,
        avgTime:
          {
            $divide:
              [
                {
                  $avg: "$difference"
                }, 60000 // in minutes
              ]
          }
      }
  }

I try to:

  1. Calculate difference between end date and start date and take it where this difference > 0
  2. Calculate average value
  3. Devide it on 60000 (to get minutes)

But I got the error: The $divide accumulator is a unary operator

How can I fix this request?

1 Answers

But I got the error: The $divide accumulator is a unary operator

The <accumulator> operator must be one of the following accumulator operators: accumulator-operator, and $divide operator is not one of them,

The $avg operator must be in root if you want to average,

Correct your syntax in $group stage and add new stage to get $divide

  {
    $group: {
      _id: null,
      avgTime: { $avg: "$difference" }
    }
  },
  {
    $project: {
      avgTime: { $divide: ["$avgTime", 60000] }
    }
  }
Related