How to display more than 20 Documents in MongoDB's Shell
Display more than 20 Documents in MongoDB Shell
By default, MongoDB's shell only displays 20 documents at a time, making it convenient for viewing small data sets but limiting for larger result sets. To see more than 20 documents in the MongoDB shell, you can either adjust the settings for larger output or use iteration functions to automatically print additional documents.
To view more than the default 20 documents in MongoDB's shell, you can use options like setting a DBQuery batch size or using JavaScript functions to iterate through the results. This ensures you can see a larger number of records at once.
Syntax and Approaches:
- Adjust Batch Size: Modify the shell's batch size using DBQuery.shellBatchSize.
- Iteration Function: Use a loop to display all results when working with larger collections.
Example:
Here are the steps for each approach:
1. Adjust Batch Size
Code:
// Set the shell batch size to display up to 50 documents at a time
DBQuery.shellBatchSize = 50
// Run a find query
db.collection.find()
Explanation:
- DBQuery.shellBatchSize = 50: Adjusts the shell’s batch size to display up to 50 documents in one output set.
- db.collection.find(): Executes the find query on the specified collection.
2. Use a While Loop for Iteration
Code:
// Store the query cursor in a variable
var cursor = db.collection.find()
// Use a while loop to print each document until the cursor is empty
while (cursor.hasNext()) {
printjson(cursor.next()) // Prints each document in JSON format
}
Where-
- var cursor = db.collection.find(): Stores the query result in a cursor, which holds the position in the document set.
- while (cursor.hasNext()): Checks if more documents are in the cursor.
- printjson(cursor.next()): Prints each document in JSON format until all documents have been displayed.
Explanation:
- Adjusting Batch Size:
- By default, MongoDB only displays 20 documents at a time in the shell to prevent data overload. Increasing DBQuery.shellBatchSize lets you view up to the specified number of documents in one batch, making it easier to examine larger data sets quickly.
- Using a While Loop:
- For larger data sets or when you want to view all results without pagination, a while loop iterates through the cursor and prints each document individually. This method ensures you see all documents without having to reset the batch size.
Additional Notes:
- Resetting Batch Size: If you set DBQuery.shellBatchSize for one session, it remains until the shell is restarted or reset manually.
- print() vs. printjson(): While print() displays output as plain text, printjson() formats it as JSON, making it easier to read for complex documents.
Example Output:
For a collection with multiple documents, using these methods would display the output in JSON format:
Output:
{ "_id": ObjectId("64b7a8d9c1b4"), "name": " Rasim Eysteinn", "age": 32, "location": "New York" } { "_id": ObjectId("64b7a8e9c1c5"), "name": " Laraine Steinarr", "age": 45, "location": "Los Angeles" } ...
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mongodb/snippets/display-more-than-20-documents-in-mongodb-shell.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics