0% found this document useful (0 votes)
72 views7 pages

Pagination in Golang and MongoDB - DEV Community

The document discusses two approaches for paginating MongoDB responses. The first approach uses the cursor methods skip and limit to return documents in batches. The second approach uses the indexed _id field to fetch documents greater than the last document's _id from the previous page.

Uploaded by

sorkunepsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views7 pages

Pagination in Golang and MongoDB - DEV Community

The document discusses two approaches for paginating MongoDB responses. The first approach uses the cursor methods skip and limit to return documents in batches. The second approach uses the indexed _id field to fetch documents greater than the last document's _id from the previous page.

Uploaded by

sorkunepsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Neeraj Kumar

Posted on Aug 23, 2023

Pagination in Golang and MongoDB


#go

MongoDB
👋 Kindness isiscontagious
a document based data store and hence pagination is one of the most
common use case of it. So when do you paginate the response? The answer is pretty
Venture
neat; youinto the heart
paginate of theyou
whenever developer world on
want to process DEVinCommunity—a
result chunks. Some common
nurturingare
scenarios zone where all coders, irrespective of their journey's stage, are
warmly invited to expand our collective tech wisdom.
Batch processing
Heartfelt
Showing“thanks” areresults
huge set of like cozy embraces—scatter
on user interfac them generously in the
comments!
Paginating on client
Your insights and server
invigorate side
DEV's are both really
experience and very expensive
amplify and should Did
our camaraderie. not be
considered. Hence pagination is generally handled at database level and databases are
this article enrich your developer life? Take a brief moment to express your
optimized for such needs to
gratitude to the author.
2 approaches through which you can easily paginate your MongoDB responses. Sample
Agreed
Document
{
"_id" : ObjectId("6936d17263623919cd5145db"),
"name" : "Neeraj Kumar",
"age" : 25
}

Approach 1: Using cursor.skip and cursor.limit


MongoDB cursor has two methods that makes paging easy; they are

cursor.skip()
cursor.limit()

skip(n) will skip n documents from the cursor while limit(n) will cap the number of
documents to be returned from the cursor. Thus combination of two naturally paginates
the response.
In Mongo Shell your pagination code looks something like

// Page 1
db.students.find().limit(10)

// Page 2
db.students.find().skip(10).limit(10)

// Page 3
db.students.find().skip(10).limit(10)
👋 Kindness is contagious
implement pagination:
Venture into the heart of the developer world on DEV Community—a
nurturing zone where all coders,
func GetPagination(limit, page int)irrespective
error { of their journey's stage, are
warmly invited:=tocontext.WithTimeout(context.Background(),
ctx, cancel expand our collective tech wisdom. 12*time.Second)
defer cancel()
Heartfelt
coll := “thanks” are like cozy embraces—scatter them generously in the
o.db.Database(mongoDatabaseName).Collection(offerCollectionName)
comments!
l := int64(limit)
Your skip
insights invigorate
:= int64(page DEV's- experience
* limit limit) and amplify our camaraderie. Did
this article
fOpt :=enrich your developer life? Take
options.FindOptions{Limit: a brief&skip}
&l, Skip: moment to express your
gratitude to the author.
curr, err := coll.Find(ctx, bson.D{{}}, &fOpt)
if err != nil {
Agreed
return result, err
}
for curr.Next(ctx) {
var el Offer
if err := curr.Decode(&el); err != nil {
log.Println(err)
}

result = append(result, el)


}

return result, nil


}

Approach 2: Using _id and limit

This approach will make effective use of default index on _id and nature of ObjectId. I
bet you didn’t know that a Mongodb ObjectId is a 12 byte structure containing

Using this property of ObjectId and also taking into consideration the fact that _id is
always indexed, we can devise following approach for pagination:

Fetch a page of documents from database


Get the document id of the last document of the page
Retrieve documents greater than that id

👋 Kindness is contagious
// Page 1
db.students.find().limit(10)
Venture into the heart of the developer world on DEV Community—a
nurturing zone
// Page 2 where all coders, irrespective of their journey's stage, are
last_id = ... # logic to get last_id
warmly invited to expand our collective tech wisdom.
db.students.find({'_id': {'$gt': last_id}}).limit(10)

Heartfelt “thanks” are like cozy embraces—scatter them generously in the


// Page 3
comments!
last_id = ... # logic to get last_id
Yourdb.students.find({'_id':
insights invigorate DEV's experience
{'$gt': and amplify
last_id}}).limit(10) our camaraderie. Did
this article enrich your developer life? Take a brief moment to express your
gratitude to the author. page int) error {
func GetPagination(limit,
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
defer cancel()
Agreed
coll := o.db.Database(mongoDatabaseName).Collection(offerCollectionName)
ctx, _ := context.WithTimeout(context.Background(), contextTimeout)

curr, err := coll.Find(ctx, bson.D{{}}, newMongoPaginate(limit,page).getPaginatedOpt


if err != nil {
return result, err
}

for curr.Next(ctx) {
var el Offer
if err := curr.Decode(&el); err != nil {
log.Println(err)
}

result = append(result, el)


}

return result, nil


}

👋 Before you go

The AI era means ongoing career reinvention. Join DEV.

It takes one minute and is worth it for your career.


👋 Kindness is contagious
Get started
Venture into the heart of the developer world on DEV Community—a
nurturing zone where all coders, irrespective of their journey's stage, are
warmly invited to expand our collective tech wisdom.

Top comments
Heartfelt (0)like cozy embraces—scatter them generously in the
“thanks” are
comments!
Code of Conduct • Report abuse
Your insights invigorate DEV's experience and amplify our camaraderie. Did
this article enrich your developer life? Take a brief moment to express your
gratitude
Sentry to the author.
PROMOTED

Agreed
👋 Kindness is contagious

Venture into the heart of the developer world on DEV Community—a


nurturing zone where all coders, irrespective of their journey's stage, are
warmlywhy
See invited4M developers
to expand consider
our collective Sentry, “not bad.”
tech wisdom.

Fixing code“thanks”
Heartfelt doesn’t have to becozy
are like the worst part of your day.
embraces—scatter Learngenerously
them how Sentryin
can
the
help.
comments!
Your insights invigorate DEV's experience and amplify our camaraderie. Did
Learn more
this article enrich your developer life? Take a brief moment to express your
gratitude to the author.

Agreed

Neeraj Kumar
My name is Neeraj Kumar, and I have over 4 years of experience in JavaScript (Node.js), Golang, Java
(Vertx), AWS, Docker, MongoDB, PostgreSQL, MySQL, JavaScript, Data Structures and Algorithms
(DSA),

LOCATION
Bengaluru
EDUCATION
B.TECH(CSE)

WORK
Full Stack Developer
JOINED
Oct 1, 2022

More from Neeraj Kumar

Hello World in Golang


#go

Introduction and Installation


#go

What is Go?
👋 #Kindness
#go devops is contagious
#beginners

Venture into the heart of the developer world on DEV Community—a


nurturingPROMOTED
PropelAuth zone where all coders, irrespective of their journey's stage, are
warmly invited to expand our collective tech wisdom.
Designing a roles & permission system that scales
Heartfelt “thanks” are like cozy embraces—scatter them generously in the
comments!
Your insights invigorate DEV's experience and amplify our camaraderie. Did
this article enrich your developer life? Take a brief moment to express your
gratitude to the author.

Agreed
👋 Kindness is contagious

Venture into the heart of the developer world on DEV Community—a


nurturing zone where all coders, irrespective of their journey's stage, are
RBAC is something
warmly that
invited to seems really
expand simple at face
our collective techvalue, because it typically does start off simple.
wisdom.
Over time, however, as customers have more unique requests and as your product gets more
Heartfelt
complex, the “thanks”
once-simpleare like cozy system
authorization embraces—scatter
can be a lot morethem generously in the
error prone.
comments!
Learn More
Your insights invigorate DEV's experience and amplify our camaraderie. Did
this article enrich your developer life? Take a brief moment to express your
gratitude to the author.

Agreed

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy