0

I want to aggregate data of my mongoose Schema so that I get data of particular month. I used match with aggregate using gte and lt operators but if only single match operator (gte or lt) given only it works.

My order schema is as follows:

   const OrderSchema = new Schema({
      _id: Schema.Types.ObjectId,
      createdAt: { type: Date, default: Date.now },
      updatedAt: { type: Date, required: false },

      //....................
     productId: { type: Schema.Types.ObjectId, ref: "products" },
     productName: { type: String, required: false },

    productQuantity: { type: Number, required: false },
    totalAmount: { type: Number, required: false },

    });

Now, I need to find total top five products sold out this month, on the basis of productQuantity. I tried using both matching createdAt field with gte with some date and lte with some date. But, if used both at once, result is not returned but when using one at a time, the result is returned.

  Order.aggregate([
{
  $match: {
    createdAt: { //schema contains createdAt field with Date.now()value
      $gte: Date("2019-04-30"),
      $lt: Date("2019-05-10")
    }
  }
  }])

  But using one at a time 
   Order.aggregate([
   {
   $match: {
    createdAt: {
      $gte: Date("2019-04-30")

    }
  }
  $group: {
     _id: "$productId",
     created: { $first: "$createdAt" },
     count: { $sum: "$productQuantity" }
   }
   }

   {
     $sort: {
      count: -1
    }
   },
    { $limit: 5 }

    }
    ])

it works.But I need to find the top sold products within a interval of month given the month. I did this way

   Order.aggregate([
   {
    $match: {
    createdAt: {
      $gte: startDate
      }
   },
   $group: {
    _id: "$productId",
    created: { $first: "$createdAt" },
    count: { $sum: "$productQuantity" }
   }
  },
  {
    $sort: {
    count: -1
    }
   },

   { $limit: 5 }
  ])

But this gives message": "Arguments must be aggregate pipeline operators" error. Why am i having such problem?

double-beep
  • 5,031
  • 17
  • 33
  • 41
luky
  • 212
  • 3
  • 12
  • 1
    So the marked duplicate is to let you know that such are highly voted set of responses are **not** incorrect, and an `$gte` and `$lte` range really is how you do it. The only question can be in your data. So I suggest that if you still believe you have a problem, then show some data in your question which can reproduce the issue. Then someone can either point out the mistake or other actual difference. – Neil Lunn May 12 '19 at 12:52
  • @NeilLunn I have edited the question. I hope that answers your doubt of duplicate. – luky May 12 '19 at 13:04
  • I solved it. I will keep this question if anyone may be facing the same kind of problem. – luky May 12 '19 at 13:24
  • Not how this works. Your *"Arguments must be aggregate pipeline operators"* was a **different question** to the one you asked, or you really should have been clear that was the question when you asked it. Basically that's a **syntax error** message due to incorrectly balanced parenthesis `{}` in the `$match` stage of the statement ( you were missing a closing `}` so the `$group` and later stages were not recognized, and the `$match` invalid ). Moreover, **that has a duplicate as well**, and it turns out I answered it. – Neil Lunn May 12 '19 at 19:38

0 Answers0