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

when unwind includes "preserveNullAndEmptyArrays" limit stage that follows it can be also included before it

    XMLWordPrintableJSON

Details

    • Fully Compatible
    • QO 2021-11-01

    Description

      Example:

      db.products.aggregate({$match:{type:"HardGood","details.name":{$exists:true}}}, {$unwind:{path:"$details", preserveNullAndEmptyArrays:true}},{$limit:100},{$project:{_id:0,"details.name":1}})
      

      will return the same results as

      db.products.aggregate({$match:{type:"HardGood","details.name":{$exists:true}}},{$limit:100}, {$unwind:{path:"$details", preserveNullAndEmptyArrays:true}},{$limit:100},{$project:{_id:0,"details.name":1}})
      

      but the second one will be able to push $limit 100 down to the query subsystem and reduce number of documents (or index entries) scanned from entire collection or entire set matching the predicate to 101.

      This is similar to pushing $sort in front of $unwind in cases where sorting is done on field that's not involved in unwinding. SERVER-26442

      Attachments

        Activity

          People

            matt.boros@mongodb.com Matt Boros
            asya.kamsky@mongodb.com Asya Kamsky
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: