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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay