MongoDB

MongoDB batchSize() Example

Hello readers, these days to increase the system performance it is important to retrieve the records from a database in batches. In Mongo world, it is pretty straightforward and in this tutorial, we will see the cursor.batchSize() method available in the Mongo database. Let’s study in brief the usage of this method.

1. Introduction

If you have installed the MongoDB application (version 3.6) on Windows or Ubuntu operating system and you wish to learn the cursor.batchSize() method then follow the below steps. It is very simple, but before moving further let’s take a look at the Mongo database and its features.

1.1 What is MongoDB?

  • MongoDB is a high-performance NoSQL database where each database has collections which in turn has documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (BSN)
  • The documents in MongoDB doesn’t need to have a schema defined beforehand. Instead, the fields (i.e. records) can be created on the go
  • Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily
  • This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability

1.1.1 Why MongoDB?

  • As a NoSQL type database, MongoDB stores the data in the form of a document. Thus, MongoDB offers more flexibility
  • This database supports search by field-name, range queries, and the regular expressions. It often provides queries to return the particular fields inside the documents
  • MongoDB offers indexes to improve the search performance within the NoSQL database
  • To offer horizontal scalability, MongoDB uses sharding by splitting the data across the many MongoDB occurrences
  • Replication: MongoDB can give high availability with the replica sets

1.2 What is a Cursor in MongoDB?

In Mongo world, a cursor is an object that allows developers to iterate through the documents of a Mongo collection. The behavior of cursor allows an automatic iteration across the results of the query; however, developers can explicitly go through the items returned in the cursor object. The below diagram lists 4 documents where the Mongo cursor will point to the first document and then iterate through all the other documents of a collection.

Fig. 1: Pictorial representation of a Cursor in Mongo collection
Fig. 1: Pictorial representation of a Cursor in Mongo collection

1.2.1 Why Cursor in MongoDB?

Cursor offers:

  • A true snapshot of a system i.e. it returns the data in batches and increases the database performance
  • It saves system memory by allowing batch inserts and updates
  • Intelligibility and Clarity on the ad-hoc and complex queries of the sequential nature having large result sets and low consistency requirements
  • Openness to work on small batches of data as developers don’t need to wait for the processing and download of the complete record-set

2. MongoDB batchSize() Example

In this Mongo tutorial, we will learn how to handle the cursor.batchSize() method provided by the Mongo database.

2.1 batchSize() method in the Mongo database

In the Mongo universe, the batchSize() method specifies the number of documents that the Mongo instance will return to the user in a single network message. In simpler words, this method controls the number of documents to be returned in each response batch from the Mongo instance. However, it will never return more documents that fit in the max batch size limit (usually 4 MB) and this is what the query syntax will look like.

Mongo database batchSize() Syntax

> db.collection_name.find().batchSize(<size_parameter>)

Where:

  • size_parameter is an optional integer type input variable that specifies the number of documents to be returned per batch

Do remember:

  • Using a batch of size 1 as specifying 1 or a negative number is like using the Mongo database limit() method
  • Using a batch of size 2 or more, represents the retrieval size of each batch of objects. This can be adjusted to optimize performance and limit the data transfer

Let’s understand this with the help of an example.

2.1.1 Start MongoDB

Start a standalone mongod instance as shown below.

Fig. 2: Start Mongo instance
Fig. 2: Start Mongo instance

2.1.2 Connect to the Mongo Instance

Connect with the mongo shell to make a connection with the MongoDB instance on the port 27017 as shown below.

Fig. 3: Connect to Mongo database
Fig. 3: Connect to Mongo database

2.1.3 Create Mongo database and collection

To begin with the implementation, we will need to create a sample database and collection. The below script creates a database called editors with a collection of exam_scores. Open the Mongo terminal and execute the script.

Database & Collection creation script

> use editors

> db.exam_scores.insertMany( [
	{"_id":0,"name":"aimee Zank","scores":[{"score":1.463179736705023,"type":"exam"},{"score":11.78273309957772,"type":"quiz"},{"score":35.8740349954354,"type":"homework"}]},
	{"_id":1,"name":"Aurelia Menendez","scores":[{"score":60.06045071030959,"type":"exam"},{"score":52.79790691903873,"type":"quiz"},{"score":71.76133439165544,"type":"homework"}]},
	{"_id":2,"name":"Corliss Zuk","scores":[{"score":67.03077096065002,"type":"exam"},{"score":6.301851677835235,"type":"quiz"},{"score":66.28344683278382,"type":"homework"}]},
	{"_id":3,"name":"Bao Ziglar","scores":[{"score":71.64343899778332,"type":"exam"},{"score":24.80221293650313,"type":"quiz"},{"score":42.26147058804812,"type":"homework"}]},
	{"_id":4,"name":"Zachary Langlais","scores":[{"score":78.68385091304332,"type":"exam"},{"score":90.2963101368042,"type":"quiz"},{"score":34.41620148042529,"type":"homework"}]},
	{"_id":5,"name":"Wilburn Spiess","scores":[{"score":44.87186330181261,"type":"exam"},{"score":25.72395114668016,"type":"quiz"},{"score":63.42288310628662,"type":"homework"}]},
	{"_id":6,"name":"Jenette Flanders","scores":[{"score":37.32285459166097,"type":"exam"},{"score":28.32634976913737,"type":"quiz"},{"score":81.57115318686338,"type":"homework"}]},
	{"_id":7,"name":"Salena Olmos","scores":[{"score":90.37826509157176,"type":"exam"},{"score":42.48780666956811,"type":"quiz"},{"score":96.52986171633331,"type":"homework"}]},
	{"_id":8,"name":"Daphne Zheng","scores":[{"score":22.13583712862635,"type":"exam"},{"score":14.63969941335069,"type":"quiz"},{"score":75.94123677556644,"type":"homework"}]},
	{"_id":9,"name":"Sanda Ryba","scores":[{"score":97.00509953654694,"type":"exam"},{"score":97.80449632538915,"type":"quiz"},{"score":25.27368532432955,"type":"homework"}]},
	{"_id":10,"name":"Denisha Cast","scores":[{"score":45.61876862259409,"type":"exam"},{"score":98.35723209418343,"type":"quiz"},{"score":55.90835657173456,"type":"homework"}]},
	{"_id":11,"name":"Marcus Blohm","scores":[{"score":78.42617835651868,"type":"exam"},{"score":82.58372817930675,"type":"quiz"},{"score":87.49924733328717,"type":"homework"}]},
	{"_id":12,"name":"Quincy Danaher","scores":[{"score":54.29841278520669,"type":"exam"},{"score":85.61270164694737,"type":"quiz"},{"score":80.40732356118075,"type":"homework"}]},
	{"_id":13,"name":"Jessika Dagenais","scores":[{"score":90.47179954427436,"type":"exam"},{"score":90.3001402468489,"type":"quiz"},{"score":95.17753772405909,"type":"homework"}]},
	{"_id":14,"name":"Alix Sherrill","scores":[{"score":25.15924151998215,"type":"exam"},{"score":68.64484047692098,"type":"quiz"},{"score":24.68462152686763,"type":"homework"}]},
	{"_id":15,"name":"Tambra Mercure","scores":[{"score":69.1565022533158,"type":"exam"},{"score":3.311794422000724,"type":"quiz"},{"score":45.03178973642521,"type":"homework"}]},
	{"_id":16,"name":"Dodie Staller","scores":[{"score":7.772386442858281,"type":"exam"},{"score":31.84300235104542,"type":"quiz"},{"score":80.52136407989194,"type":"homework"}]},
	{"_id":17,"name":"Fletcher Mcconnell","scores":[{"score":39.41011069729274,"type":"exam"},{"score":81.13270307809924,"type":"quiz"},{"score":97.70116640402922,"type":"homework"}]},
	{"_id":18,"name":"Verdell Sowinski","scores":[{"score":62.12870233109035,"type":"exam"},{"score":84.74586220889356,"type":"quiz"},{"score":81.58947824932574,"type":"homework"}]},
	{"_id":19,"name":"Gisela Levin","scores":[{"score":44.51211101958831,"type":"exam"},{"score":0.6578497966368002,"type":"quiz"},{"score":93.36341655949683,"type":"homework"}]},
	{"_id":20,"name":"Tressa Schwing","scores":[{"score":42.17439799514388,"type":"exam"},{"score":71.99314840599558,"type":"quiz"},{"score":81.23972632069464,"type":"homework"}]},
	{"_id":21,"name":"Rosana Vales","scores":[{"score":46.2289476258328,"type":"exam"},{"score":98.34164225207036,"type":"quiz"},{"score":36.18769746805938,"type":"homework"}]},
	{"_id":22,"name":"Margart Vitello","scores":[{"score":75.04996547553947,"type":"exam"},{"score":10.23046475899236,"type":"quiz"},{"score":96.72520512117761,"type":"homework"}]},
	{"_id":23,"name":"Tamika Schildgen","scores":[{"score":45.65432764125526,"type":"exam"},{"score":64.32927049658846,"type":"quiz"},{"score":83.53933351660562,"type":"homework"}]},
	{"_id":24,"name":"Jesusa Rickenbacker","scores":[{"score":86.0319702155683,"type":"exam"},{"score":1.967495200433389,"type":"quiz"},{"score":61.10861071547914,"type":"homework"}]},
	{"_id":25,"name":"Rudolph Domingo","scores":[{"score":74.75289335591543,"type":"exam"},{"score":38.5413647805495,"type":"quiz"},{"score":35.2554340953413,"type":"homework"}]},
	{"_id":26,"name":"Jonie Raby","scores":[{"score":19.17861192576963,"type":"exam"},{"score":76.3890359749654,"type":"quiz"},{"score":44.39605672647002,"type":"homework"}]},
	{"_id":27,"name":"Edgar Sarkis","scores":[{"score":8.606983261043888,"type":"exam"},{"score":58.71180464203724,"type":"quiz"},{"score":15.33726210596508,"type":"homework"}]},
	{"_id":28,"name":"Laureen Salomone","scores":[{"score":3.677565278992456,"type":"exam"},{"score":7.119462599229987,"type":"quiz"},{"score":82.87308922617427,"type":"homework"}]},
	{"_id":29,"name":"Gwyneth Garling","scores":[{"score":48.36644963899371,"type":"exam"},{"score":10.37827022865908,"type":"quiz"},{"score":93.26639335532833,"type":"homework"}]},
	{"_id":30,"name":"Kaila Deibler","scores":[{"score":15.89771199662455,"type":"exam"},{"score":56.93965183412178,"type":"quiz"},{"score":66.64493295066322,"type":"homework"}]},
	{"_id":31,"name":"Tandra Meadows","scores":[{"score":24.90138146001744,"type":"exam"},{"score":28.8266541837344,"type":"quiz"},{"score":97.16831550665721,"type":"homework"}]},
	{"_id":32,"name":"Gwen Honig","scores":[{"score":87.14345376886205,"type":"exam"},{"score":99.45824441135635,"type":"quiz"},{"score":76.66460454219344,"type":"homework"}]},
	{"_id":33,"name":"Sadie Jernigan","scores":[{"score":73.15861249943812,"type":"exam"},{"score":2.987718065941702,"type":"quiz"},{"score":82.54104198590488,"type":"homework"}]},
	{"_id":34,"name":"Carli Belvins","scores":[{"score":7.112266875518214,"type":"exam"},{"score":67.734668378287,"type":"quiz"},{"score":88.99855402666871,"type":"homework"}]},
	{"_id":35,"name":"Synthia Labelle","scores":[{"score":27.22049103148209,"type":"exam"},{"score":31.28760039265919,"type":"quiz"},{"score":79.23285425688643,"type":"homework"}]},
	{"_id":36,"name":"Eugene Magdaleno","scores":[{"score":73.055900093666,"type":"exam"},{"score":79.85621560462026,"type":"quiz"},{"score":66.09143669040472,"type":"homework"}]},
	{"_id":37,"name":"Meagan Oakes","scores":[{"score":86.06759716616264,"type":"exam"},{"score":79.45097452834857,"type":"quiz"},{"score":28.41090281547689,"type":"homework"}]},
	{"_id":38,"name":"Richelle Siemers","scores":[{"score":34.64373397163318,"type":"exam"},{"score":91.46799649446983,"type":"quiz"},{"score":56.12615074082559,"type":"homework"}]},
	{"_id":39,"name":"Mariette Batdorf","scores":[{"score":0.04381116979284005,"type":"exam"},{"score":90.25774974259562,"type":"quiz"},{"score":65.88612319625227,"type":"homework"}]},
	{"_id":40,"name":"Rachell Aman","scores":[{"score":84.53009035375172,"type":"exam"},{"score":25.25568126160764,"type":"quiz"},{"score":70.42062575402956,"type":"homework"}]},
	{"_id":41,"name":"Aleida Elsass","scores":[{"score":28.02518041693717,"type":"exam"},{"score":95.25243105389065,"type":"quiz"},{"score":68.05980405338909,"type":"homework"}]},
	{"_id":42,"name":"Kayce Kenyon","scores":[{"score":44.62441703708117,"type":"exam"},{"score":27.38208798553111,"type":"quiz"},{"score":97.43587143437509,"type":"homework"}]},
	{"_id":43,"name":"Ernestine Macfarland","scores":[{"score":15.29147856258362,"type":"exam"},{"score":78.40698797039501,"type":"quiz"},{"score":31.03031764716336,"type":"homework"}]},
	{"_id":44,"name":"Houston Valenti","scores":[{"score":98.06441387027331,"type":"exam"},{"score":0.8760893342659504,"type":"quiz"},{"score":15.2177618920215,"type":"homework"}]},
	{"_id":45,"name":"Terica Brugger","scores":[{"score":42.1011312120801,"type":"exam"},{"score":41.73654145887228,"type":"quiz"},{"score":18.91287189072117,"type":"homework"}]},
	{"_id":46,"name":"Lady Lefevers","scores":[{"score":16.89237820123443,"type":"exam"},{"score":65.97505910406456,"type":"quiz"},{"score":48.42527123437286,"type":"homework"}]},
	{"_id":47,"name":"Kurtis Jiles","scores":[{"score":92.96916908741805,"type":"exam"},{"score":22.86854192921203,"type":"quiz"},{"score":31.89793879453222,"type":"homework"}]},
	{"_id":48,"name":"Barbera Lippman","scores":[{"score":35.43490750932609,"type":"exam"},{"score":97.42074160188449,"type":"quiz"},{"score":74.1092960902528,"type":"homework"}]},
	{"_id":49,"name":"Dinah Sauve","scores":[{"score":96.64807532447064,"type":"exam"},{"score":14.56470882270576,"type":"quiz"},{"score":72.00519420743191,"type":"homework"}]}
] )

The script gives the below output.

Fig. 4: Database & Collection creation
Fig. 4: Database & Collection creation

2.1.4 Check Mongo database and collection

If the script works well, the database and the collection will be shown in the Mongo Workbench. Using the db.collection_name.find() command the documents of a collection will appear as below.

Fig. 5: Mongo database & collection
Fig. 5: Mongo database & collection

Here the exam_scores collection has 50 sample documents (i.e. records).

2.1.5 cursor.batchSize() method

Now, go back to the Mongo shell and use the batchSize(<size_parameter>) method to display the specified number of documents.

Query 1

> db.exam_scores.find().batchSize(10)

This command sets the batch size to 10 for the results of a find() query. The batchSize() method does not change the output in the Mongo shell, which, by default iterate over the first 20 documents.

Fig. 6: Mongo database batchSize() method
Fig. 6: Mongo database batchSize() method

Do note, the find() method displays the output in a non-structured pattern. If developers want to display the output in an attractive structured pattern, they can append the pretty() method to the find() query as shown below.

Modified Query 1

> db.exam_scores.find().batchSize(10).pretty()

The pretty() method modifies the output in an attractive way as shown below.

Fig. 7: Mongo database pretty() method
Fig. 7: Mongo database pretty() method

That’s all for this post. Happy Learning!!

3. Conclusion

In this tutorial, we learned about the batchSize() method of the Mongo database. Developers can download the sample commands in the Downloads section.

4. Download the Eclipse Project

This was an example of the batchSize() method available in the Mongo database.

Download
You can download the full source code of this example here: MongoDbCursorbatchSize()Method

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Manish
Manish
5 years ago

The batchSize() method does not change the output in the Mongo shell, which, by default iterate over the first 20 documents.

If batchSize() does not changes the out put then what is the use of the function even after using this function all we do get is all the documents? What is the purpose of this function then? Also, if it is not possible in batchSize() then how can we limit the Output to a particular number?

Back to top button