WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Advanced · question 55 of 100

What are the different types of MongoDB cursors, and how do they affect query performance?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

MongoDB offers three types of cursors – Basic, Cursor Snapshot and Tailable. Each of these cursors have their own unique characteristics and affect the query performance in different ways.

1. Basic Cursor: Basic cursor represents the default behavior of the MongoDB query. In a basic cursor, the client issues a query to the server and the database returns the results to client in batches. The size of each batch is determined by the server, and it’s default value is 101 records.

To retrieve data from the basic cursor, we use the ‘findOne()‘ or ‘next()‘ method on the cursor. However, if the result set is very large, it may cause performance issues as it can potentially fill up the memory of the application.

2. Cursor Snapshot: When we have to iterate over a large collection and the data inside it is being modified by another process, we should use a Snapshot Cursor to avoid performance issues caused by collection size or modification. A Snapshot Cursor returns the data on the server, as it exists at the start of the query, and that data remains static.

A Snapshot Cursor disables the database from updating the data returned by the cursor. This is useful when we need to make queries on a collection without allowing any changes to the collection until the query is completed.

Consider the following example where we want a static result set for an inventory collection:

const inventory = db.inventory.find({})
const snapshotCursor = inventory.snapshot()

snapshotCursor.forEach(printjson)

3. Tailable Cursor: Tailable Cursor is a special type of cursor that keeps on fetching data from a collection even after the client has exhausted the initial results set. This type of cursor is used to maintain a stream of information.

A tailable cursor is used when we want to create a capped collection, which has limited size and always returns the items in the order in which they were inserted. It continues to return data to the client as it’s inserted by other clients or processes.

Tailable Cursors also allows us to use server side listeners to affect application behavior when the cursors insert new documents. This is useful when we want to stream documents in real-time to an application without polling the database incessantly.

Consider the following example where we insert documents into a capped collection and read them:

db.createCollection("my_collection", {capped: true, size: 1000, max: 3})

db.my_collection.insert({"name": "document1"})
db.my_collection.insert({"name": "document2"})
db.my_collection.insert({"name": "document3"})

const cursor = db.my_collection.find().sort({$natural: 1}).tailable(true)

while(cursor.hasNext()){
  let doc = cursor.next()
  printjson(doc)
  sleep(1000)
}

In conclusion, Basic Cursor are ideal for small collections whereas Snapshot and Tailable Cursors are useful for optimizing specific use cases. Through proper evaluation of requirements and using the right cursor type, we can ensure the optimised use of resources and improved query execution time.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics