DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Yaşar Δ°Γ‡LΔ°
Yaşar Δ°Γ‡LΔ°

Posted on

Bongo Pagination

In our previous article, we made a collection with bongo.

If you haven't read it, you can look here Make the collection find using Go bongo

Now we will paginate our query.

we continue the connection in the same way

  • Perpage 20
  • Active Page 1
package main

import (
  "log"

  "github.com/globalsign/mgo/bson"
  "github.com/go-bongo/bongo"
)

type User struct {
  bongo.DocumentBase `bson:",inline"`
  Username          string
}


func main() {

  // Bongo Connection Config
  config := &bongo.Config{
    ConnectionString: "localhost",
    Database:         "test",
  }

  connection, err := bongo.Connect(config)

  if err != nil {
    log.Fatal(err)
  }

  result := connection.Collection("users").Find(bson.M{})

  // Perpage = 20, activePage = 1
  info, _ := result.Paginate(20, 1)

  users := make([]User, info.RecordsOnPage)

  for i := 0; i < info.RecordsOnPage; i++ {
    _ = result.Next(&users[i])
  }


  log.Println(users)
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈