DEV Community

Yaşar İÇLİ
Yaşar İÇLİ

Posted on

3 2

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay