0
0
MongoDBquery~20 mins

Pretty printing and cursor behavior in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pretty Printing and Cursor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of find() with pretty()
What is the output of the following MongoDB query on a collection with documents {"name": "Alice", "age": 30} and {"name": "Bob", "age": 25}?
MongoDB
db.users.find().pretty()
ASyntaxError: pretty() is not a function
B{ "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 }
C[{ "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 }]
D
{
  "name": "Alice",
  "age": 30
}
{
  "name": "Bob",
  "age": 25
}
Attempts:
2 left
💡 Hint
pretty() formats the output with indentation and line breaks.
🧠 Conceptual
intermediate
2:00remaining
Cursor behavior after iteration
After running the following code, what happens if you try to iterate over the cursor again?
MongoDB
var cursor = db.users.find();
while(cursor.hasNext()) {
  printjson(cursor.next());
}
// Now iterate again
while(cursor.hasNext()) {
  printjson(cursor.next());
}
AThe second loop resets the cursor automatically and prints all documents.
BThe second loop prints no documents because the cursor is exhausted.
CThe second loop throws a runtime error because the cursor is closed.
DThe second loop prints the same documents again.
Attempts:
2 left
💡 Hint
Think about how cursors work in MongoDB after reading all documents.
📝 Syntax
advanced
2:00remaining
Correct usage of pretty() with find()
Which of the following MongoDB shell commands correctly uses pretty() to format the output of a find query?
Adb.collection.find().pretty
Bdb.collection.pretty().find()
Cdb.collection.find().pretty()
Ddb.collection.find(pretty())
Attempts:
2 left
💡 Hint
pretty() is a method called on the cursor returned by find().
optimization
advanced
2:00remaining
Improving performance when pretty printing large query results
You want to pretty print a large number of documents from a collection. Which approach is best to avoid performance issues?
AUse find().limit(10).pretty() to print only a small number of documents.
BUse find().pretty() with no limit and print all documents at once.
CUse find().pretty() and store all results in an array before printing.
DUse find() without pretty() and format the output manually in the application.
Attempts:
2 left
💡 Hint
Consider how much data is processed and displayed at once.
🔧 Debug
expert
2:00remaining
Diagnosing cursor error after pretty() usage
You run this code in the MongoDB shell: var cursor = db.users.find().pretty(); printjson(cursor.next()); printjson(cursor.next()); But you get an error: "TypeError: cursor.next is not a function". What is the cause?
Apretty() returns a string, not a cursor, so next() is undefined.
BThe cursor is exhausted after the first next() call, so the second fails.
CThe find() method must be called with a query object to use next().
DThe pretty() method must be called after next(), not before.
Attempts:
2 left
💡 Hint
Check what pretty() returns compared to find().