I am using official mongo driver for Golang: go.mongodb.org/mongo-driver/mongo
Preface In this driver, I could not found any method for returning one single object from aggregation query. driver aggregation documentation
Problem
The problem I am facing with this is if I have some documents which should be filtered and only first one should be returned, then I forcefully need to get all documents and return document on 0 index. In my knowledge, this is not optimized.
I have only one method for aggregation here, which returns cursor of multiple objects:
Is it possible to get single object in aggregation in this driver ?
Aggregation always returns a list of documents, but you may use the $limit
stage to only return one document.
bson.M{"$limit": 1}