Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter with date in mongodb

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}
])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 …

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading