MongoDB
 SQL >> Datenbank >  >> NoSQL >> MongoDB

Max und Group By in MongoDB

Die SQL-Abfrage mit Unterabfrage wird in MongoDB über $match umgesetzt:

db.getCollection("_core.data").aggregate([
  {
    "$match": {
      "TFN": { "$in": tmpTFNList },
      "TrendDate": { "$gte": startDate, "$lte": endDate }
    }
  }
])

Maximum pro TFN:

db.getCollection("_core.data").aggregate([
  {
    "$match": {
      "TFN": { "$in": tmpTFNList },
      "TrendDate": { "$gte": startDate, "$lte": endDate }
    }
  },
  {
    "$group": {
      "_id": "$TFN",
      "MaxImpression": { "$max": "$Impression" }
    }
  }
])

Top 5 ergänzen Sie mit $limit und $project. Für Maximum nach Datum zuerst mit $sort und dann $first verwenden.