From 15ba3e93d6218826fe0fcb2a69ffb7e857114fc2 Mon Sep 17 00:00:00 2001 From: mountain Date: Tue, 20 Jun 2023 09:59:11 +0900 Subject: [PATCH] =?UTF-8?q?mongo=20allas=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mongo.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/mongo.go b/mongo.go index 93ec9e5..e38ed29 100644 --- a/mongo.go +++ b/mongo.go @@ -135,20 +135,25 @@ func (mc MongoClient) Collection(collname CollectionName) *mongo.Collection { return mc.db.Collection(string(collname)) } -func (mc MongoClient) All(coll CollectionName, opts ...*options.FindOptions) ([]bson.M, error) { +func (mc MongoClient) AllAs(coll CollectionName, output any, opts ...*options.FindOptions) error { cursor, err := mc.Collection(coll).Find(context.Background(), bson.D{}, opts...) if err != nil { - return nil, err + return err } defer cursor.Close(context.Background()) - var all []bson.M - err = cursor.All(context.Background(), &all) + err = cursor.All(context.Background(), output) if err != nil { - return nil, err + return err } - return all, nil + return nil +} + +func (mc MongoClient) All(coll CollectionName, opts ...*options.FindOptions) ([]bson.M, error) { + var all []bson.M + err := mc.AllAs(coll, &all, opts...) + return all, err } func (mc MongoClient) FindOneAndDelete(coll CollectionName, filter bson.M, opts ...*options.FindOneAndDeleteOptions) (bson.M, error) {