size() (Count on Cursor)
Last updated: 2026-10-09
size() (Count on Cursor)
size() returns the number of documents in a cursor: `db.users.find().size()`. Its semantics mostly match count(), except that size() respects an applied limit() while count() does not. The method is deprecated — prefer `db.users.countDocuments()`. Know it mainly for reading legacy code.
| Category | Cursor |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.find(<filter>).size()
Returns:Number of documents in the cursor (integer).
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// size: count matching documents
const n = db.users.find({ age: { $gte: 18 } }).size()
print(n)
Output:
42
📌 Notes
- Deprecated — use countDocuments() in new code
- Unlike count(), size() honors a preceding limit()
- Does not consume the cursor; iteration can continue
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between size and count?
ABoth count cursor results; the one difference is size() honors limit() while count() ignores it.
QIs size() still usable?
AIt works but is deprecated; official guidance is countDocuments(), so avoid size() in new code.
QWhat does find().limit(5).size() return?
AIt returns 5 (when at least 5 match), because size() accounts for the limit.