DEV Community

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)