Tsallaka zuwa abun ciki
Justice Tech School

Cursors and Batches

find returns a cursor that fetches documents in batches. Use toArray for small results, iterate for large ones and avoid loading millions into memory.

Example
const cursor = db.students.find({ age: { $gt: 20 } })
while (cursor.hasNext()) {
  printjson(cursor.next())
}
db.students.find().toArray()

Exercise

Convert to an array.

cursor.()

Quiz Test

Why not use toArray on huge results?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.