[SERVER-39108] Make $limit accept 0 for all documents to be passed Created: 20/Jan/19  Updated: 22/Apr/19  Resolved: 12/Feb/19

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

Type: Improvement Priority: Minor - P4
Reporter: Markus Mahlberg Assignee: Asya Kamsky
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-40759 New Agg Metadata Source to Generate A... Backlog
Participants:

 Description   

`$limit`, as it is now, accepts positive integers to be passed to limit the documents handed over to the next stage.

However, to dynamically set up aggregations in programs, one might want to pass a certain amount of documents for testing, while per default all documents should be processed.

In order to be able to do it now, one needs to modify the structure of the aggregation passed in order to be able to do that. The below example is given in Go, but it is applicable for all the other languages as well, I presume.

func createAggregationNow(limit int) []bson.M {
	var agg []bson.M
 
	agg = []bson.M{
		bson.M{"$match": bson.M{"date": bson.M{"$lte": time.Now()}}},
		bson.M{"$sort": bson.M{"date": -1}},
	}
 
	if limit > 0 {
		agg = append(agg, bson.M{"$limit": limit})
	}
 
	remaining := []bson.M{
		bson.M{"$unwind": "$items"},
		bson.M{"$out": "foobar"},
	}
	return append(agg, remaining...)
}

Accepting 0 as a value for `$limit` which would simply pass all documents would result in cleaner code:

func createAggregationBetter(limit int) []bson.M {
	return []bson.M{
		bson.M{"$match": bson.M{"date": bson.M{"$lte": time.Now()}}},
		bson.M{"$sort": bson.M{"date": -1}},
		bson.M{"$limit": limit},
		bson.M{"$unwind": "$items"},
		bson.M{"$out": "foobar"},
	}
}

I guess a `$limit` with value 0 could actually be optimized out of the pipeline when it is parsed, but that is just a guess.



 Comments   
Comment by Asya Kamsky [ 06/Feb/19 ]

markus.mahlberg@icloud.com we don't think this would be expected by users as it's possible that someone else may think that limit of zero means no documents need to be passed through the pipeline.

In fact in SQL in several databases LIMIT 0 means no rows and it's used to get back metadata for the table being queried without any data. There are discussions of this breaking in databases where "limit 0" means all rows.

Comment by Ramon Fernandez Marina [ 21/Jan/19 ]

Thanks for your report markus.mahlberg@icloud.com; sending it to the Query team for consideration.

Regards,
Ramón.

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