Original post

package main

import (
        "fmt"
        "github.com/go-acme/lego/log"
        "github.com/miguelpragier/mongohelper"
        "go.mongodb.org/mongo-driver/bson"
        "go.mongodb.org/mongo-driver/bson/primitive"
        "time"
)

const (
        testDB         = `mongohelper`
        testCollection = `testsuite`
)

var testConnectionString = fmt.Sprintf("mongodb://127.0.0.1:27017/%s", testDB)

type testDocStruct struct {
        ID   primitive.ObjectID `bson:"_id,omitempty"`
        Name string             `bson:"name,omitempty"`
        TS   time.Time          `bson:"ts,omitempty"`
        N    int                `bson:"n,omitempty"`
}

func main() {
        const (
                connTimeoutSeconds         = 10 // Time to wait for the first connection
                execTimeoutSeconds         = 10 // Time to wait for execution
                reconnTimeoutSeconds       = 10 // Time between reconnection attempts
                reconnAttemptsLimit        = 0  // Top limit for (re)connection attempts
                reconnAttemptsLimitMinutes = 5  // limit time trying to reconnect
                insistOnFailure            = false
                logDebugMessages           = true
        )

        var (
                mdb             mongohelper.Link
                lastInsertedOID primitive.ObjectID
        )

        log.Println("Connecting db...")

        if _m, err := mongohelper.New(testConnectionString, mongohelper.OptionsNew(connTimeoutSeconds, execTimeoutSeconds, reconnTimeoutSeconds, reconnAttemptsLimit, reconnAttemptsLimitMinutes, insistOnFailure, logDebugMessages)); err != nil {
                log.Fatal(err)
        } else {
                mdb = *_m
        }

        x := testDocStruct{
                ID:   primitive.NewObjectID(),
                Name: "testing insertone",
                TS:   time.Now(),
                N:    125,
        }

        log.Println("Inserting one doc")

        if s, err := mdb.InsertOne(testDB, testCollection, &x); err != nil {
                log.Println(err)
        } else {
                log.Printf("inserted oID: %sn", s)
        }

        var a []interface{}

        for i := 0; i < 100; i++ {
                x := testDocStruct{
                        ID:   primitive.NewObjectID(),
                        Name: fmt.Sprintf("test #%d", i),
                        TS:   time.Now().Add(time.Duration(i) * time.Hour),
                        N:    i * 2,
                }

                a = append(a, x)

                lastInsertedOID = x.ID
        }

        log.Println("Inserting many doc")

        if ioids, err := mdb.InsertMany(testDB, testCollection, a); err != nil {
                log.Println(err)
        } else {
                log.Printf("inserted oIDs: %dn", len(ioids))
        }

        log.Println("testing .CountDocs() with empty filter")

        if n, err := mdb.CountDocs(testDB, testCollection, bson.M{}); err != nil {
                log.Println(err)
        } else {
                log.Printf("found %d docsn", n)
        }

        log.Println("testing .CountDocs() with ObjectId filter")

        if n, err := mdb.CountDocs(testDB, testCollection, bson.M{"_id": lastInsertedOID}); err != nil {
                log.Println(err)
        } else {
                log.Printf("searched oID %s and found %d docsn", lastInsertedOID.Hex(), n)
        }

        log.Println("updating one doc")

        if n, err := mdb.UpdateOne(testDB, testCollection, bson.M{"_id": lastInsertedOID}, bson.M{"$set": bson.M{"xyz": "abc"}}); err != nil {
                log.Println(err)
        } else {
                log.Printf("%d doc updatedn", n)
        }

        var docsToDelete int64

        log.Println("updating many docs")

        if n, err := mdb.UpdateMany(testDB, testCollection, bson.M{"n": 8}, bson.M{"$set": bson.M{"name": "delete me!"}}); err != nil {
                log.Println(err)
        } else {
                docsToDelete = n

                log.Printf("updated docs flagged to be deleted: %dn", docsToDelete)
        }

        log.Println("deleting one doc")

        if n, err := mdb.DeleteOne(testDB, testCollection, bson.M{"xyz": "abc"}); err != nil {
                log.Println(err)
        } else {
                log.Printf("delete %d document %sn", n, lastInsertedOID.Hex())
        }

        log.Println("deleting many docs")

        if n, err := mdb.DeleteMany(testDB, testCollection, bson.M{"name": "delete me!"}); err != nil {
                log.Println(err)
        } else {
                log.Printf("deleted docs %dn", n)

                if n != docsToDelete {
                        log.Printf("expected deleteable docs: %dn", docsToDelete)
                }
        }
}