Home > Software engineering >  I have a dataset like this and I need a query in MongoDB
I have a dataset like this and I need a query in MongoDB

Time:05-19

{ Flight: "1", ArrDelayMinutes :"3.0"} ,

{ Flight:"1", ArrDelayMinutes:"16.0"},

{Flight:"2", ArrDelatMinutes:"15.0"}

List the total number of delayed arrival flights in the dataset. A delayed arrival flight or plane is one whose ArrDelayMinutes is greater than 15

CodePudding user response:

You can use aggregation like following

Here is the code

db.collection.aggregate([
  {
    "$match": {
      $expr: {
        $gt: [
          { $toDouble: "$ArrDelayMinutes" },
          15.0
        ]
      }
    }
  },
  {
    "$group": {
      "_id": null,
      "total": { "$sum": 1 }
    }
  }
])

Working Mongo playground

  • Related