-
Type:
Improvement
-
Resolution: Done
-
Priority:
Minor - P4
-
Affects Version/s: 2.5.0
-
Component/s: None
Description
To quote the available documentation on (*CountOptionsBuilder) SetLimit(i int64):
SetLimit sets the value for the Limit field. Specifies the maximum number of documents to count. The default value is 0, which means that there is no limit and all documents matching the filter will be counted.
One would assume that passing the default value of 0 would yield no difference in behaviour to simply not calling SetLimit().
In actuality, SetLimit() does NOT allow for it's parameter to be zero and will return an error in such case.
Detailed steps to reproduce the problem?
Run the following query on any collection:
filters := bson.D{} // irrelevant
coll := db.Collection("collname") // default collection
c, err := coll.CountDocuments(context.Background(), filters, options.Count().SetLimit(0))
And see that err is
mongo.CommandError {Code: 15958, Message: "the limit must be positive", Labels: []string len: 0, cap: 0, nil, Name: "Location15958", Wrapped: error(go.mongodb.org/mongo-driver/v2/x/mongo/driver.Error) {Code: 15958, Message: "the limit must be positive", Labels: []string len: 0, cap: 0, nil, Name: "Location15958", Wrapped: error nil, TopologyVersion: *go.mongodb.org/mongo-driver/v2/x/mongo/driver/description.TopologyVersion nil, Raw: go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore.Document len: 219, cap: 219, [<ommited by me>]}, Raw: go.mongodb.org/mongo-driver/v2/bson.Raw len: 219, cap: 219, [<ommited by me>], wrappedMsgOnly: true}
Definition of done: what must be done to consider the task complete?
I would expect either the documentation to reflect the actual behaviour or the driver to ignore the SetLimit() call when the argument being passed is zero.
The exact Go version used, with patch level:
$ go version
go version go1.26.0 windows/amd64
The exact version of the Go driver used:
$ go list -m go.mongodb.org/mongo-driver/v2
go.mongodb.org/mongo-driver/v2 v2.5.0
Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.
It's being hosted on Atlas:
- M0 Cluster;
- Version 8.0.20;
- us-east-1;
- Replica Set - 3 nodes.
The operating system and version (e.g. Windows 7, OSX 10.8, ...)
Driver is running on Windows 10 22H2.