[SERVER-27598] when unwind includes "preserveNullAndEmptyArrays" limit stage that follows it can be also included before it Created: 06/Jan/17  Updated: 27/Oct/21  Resolved: 27/Oct/21

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 5.2.0

Type: Improvement Priority: Major - P3
Reporter: Asya Kamsky Assignee: Matt Boros
Resolution: Done Votes: 0
Labels: neweng, optimization
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: QO 2021-11-01
Participants:

 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



 Comments   
Comment by Githook User [ 27/Oct/21 ]

Author:

{'name': 'Matt Boros', 'email': 'matt.boros@mongodb.com'}

Message: SERVER-27598 Added logic to allow limit to duplicate and swap before unwind. Altered unwind and sort logic to check for top-k sort.
Branch: master
https://github.com/mongodb/mongo/commit/c034afdd3d2513adb8750263459f0a67302106e4

Generated at Thu Feb 08 04:15:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.