Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
miilord committed May 6, 2024
1 parent 0d43c8f commit d447bdd
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ MODM is a MongoDB wrapper built on top of the mongo-go-driver, leveraging the po

When using mongodb, the typical approach is to define models and collections as follows:

### With mongo-go-driver

```go
type User struct {
DefaultField `bson:",inline"`
Expand All @@ -36,6 +38,8 @@ type User struct {
}

coll := db.Collection("users")

// When using find(), it's necessary to predefine the return structure and manually iterate through the cursor
users := make([]*User, 0)
cursor, err := coll.Find(context.TODO(), bson.D{})
if err != nil {
Expand All @@ -46,7 +50,9 @@ if err = cursor.All(context.TODO(), &users); err != nil {
}
```

In contrast, using modm can greatly simplify the process:
### With modm

On the other hand, modm offers a simpler approach:

```go
type User struct {
Expand All @@ -55,7 +61,9 @@ type User struct {
Age int `bson:"age,omitempty" json:"age"`
}

coll := NewRepo[*User](db.Collection("users"))
coll := modm.NewRepo[*User](db.Collection("users"))

// No need to predefine the return structure, and the cursor management is automatic
users, err := coll.Find(context.TODO(), bson.D{})
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit d447bdd

Please sign in to comment.