Wrappers for go mongodb driver methods using generics to remove the redundant unmarshal calls
go get github.com/aliforever/go-mongolio
- Define the accounts collection:
package main
import "github.com/aliforever/go-mongolio"
var Accounts *mongolio.C[Account]
type Account struct {
ID int
UserID int
Username string
}
func (Account) CollectionName() string {
return "accounts"
}
- Initialize the db and
Accounts
func main() {
var err error
var client *mongo.Client
client, err = mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://127.0.0.1:27017"))
if err != nil {
panic(err)
}
db := client.Database("mydb")
Accounts = mongorm.Collection[Account](db)
}
- Find an account by username:
account, err := Accounts.FindOne(bson.M{"username": "admin"})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%#v\n", account)