How to filter with date in mongodb

Advertisements

Can anyone tell me why i get null result ?
here is the JSON file

{
    "_id": 12346,
    "Shipped Time": "1days",
    "items": ["558", "561", "564", "567", "568"],
    "Invoice": {"_id": 4444,"Totalprice": 19160, "Date": { "$date": 
      "2021-08-12T22:00:00.000Z"}
    }
}

What i try !

 db.orders.aggregate([
    { $unwind: "$Invoice" }, 
    {$match: {Date: {$gte: ISODate("2021-06-16T23:00:00.000Z")}}},
    { $group: { _id: "$_id", Totalprice: { $max: "$Invoice.Totalprice" }}}, 
    { $sort: { "Totalprice": -1 } },
    {$limit:10}
])

>Solution :

Check this example playground

 db.collection.aggregate([
  {
     $match: {
       "Invoice.Date": {
        $gte: ISODate("2021-06-16T23:00:00.000Z")
    }
   }
  },
  {
    $group: {
        _id: "$_id",
      Totalprice: {
      $max: "$Invoice.Totalprice"
      }
    }
  },
  {
  $sort: {
  "Totalprice": -1
  }
  },
  {
   $limit: 10
  } 
  ])

As @wernfried-domscheit commented $unwind is unnecessary for objects
also the _id in general is unique so above query do not make alot of sence unless the _id is not unique …

Leave a ReplyCancel reply