MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

MongoDB shell variable printing only once

I am learning about variables in the m001 course. So I tried a few commands using variables.

But the variable (document) is printing only once. After some searching on the internet, I found this.

find() is a cursor, It can hold the variable only once.

This might be known to all. But as a beginner, I struggled for a while. I hope this helps beginners like me.

The db.collection.find() returns a cursor. You can apply any of these cursor methods on the returned cursor. Some of the cursor methods often used are the forEach, explain, hasNext (and next), toArray, etc.

The db.collection.findOne() returns one document that matches the filter condition, or else a null.

1 Like