Skip to content

Commit

Permalink
feat(count): Add count method to document package
Browse files Browse the repository at this point in the history
  • Loading branch information
SCedricThomas committed Feb 3, 2023
1 parent 3ab83a0 commit 02592e5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mongo/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ func WhereUnscoped(ctx context.Context, collectionName string, query bson.M, dat
return nil
}

func Count(ctx context.Context, collectionName string, query bson.M) (int, error) {
if query == nil {
query = bson.M{}
}
if _, ok := query["deleted_at"]; !ok {
query["deleted_at"] = nil
}

return CountUnscoped(ctx, collectionName, query)
}

func CountUnscoped(ctx context.Context, collectionName string, query bson.M) (int, error) {
log := logger.Get(ctx)
c := mongo.Session(log).Clone().DB("").C(collectionName)

if query == nil {
query = bson.M{}
}

return c.Find(query).Count()
}

func WhereIter(ctx context.Context, collectionName string, query bson.M, fun func(*mgo.Iter) error, sortFields ...SortField) error {
if query == nil {
query = bson.M{}
Expand Down

0 comments on commit 02592e5

Please sign in to comment.