Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-61656

Last point TS Opt M6: $topn(1) and/or $top

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.0.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Fully Compatible

      A query like this:

       

      db.coll.aggregate([
        {$group: {
          _id: "$meta.a",
           mostRecent: {$topN: {
             n: 1,
             sortBy: {ts: -1},
             output: {
               ts: "$ts",
               measureA: "$measureA",
               measureB: "$measureB"
             }
           }}
        }}
      ])
      

      or

      db.coll.aggregate([
        {$group: {
          _id: "$meta.a",
           mostRecent: {$top: {
             sortBy: {ts: -1},
             output: {
               ts: "$ts",
               measureA: "$measureA",
               measureB: "$measureB"
             }
           }}
        }}
      ])
      

      Will be rewritten to something like this to get the last point optimization:

      db.measurements.aggregate([
         {$sort: {"meta.a": 1, "ts": -1}},
         {$group: {
           _id: "$sensorId”,
           ts: {$first: "$ts”},
           measureA: {$first: "$measureA"},
           measureB: {$first: "$measureB"}
         }}
      ])

       

            Assignee:
            alya.berciu@mongodb.com Alya Berciu
            Reporter:
            ruslan.abdulkhalikov@mongodb.com Ruslan Abdulkhalikov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: