findOne() (Query a Single Document)

Last updated: 2026-10-09

findOne() (Query a Single Document)

findOne() returns the first document matching a filter directly as a document, not a cursor, e.g. `db.users.findOne({ name: 'Alice' })`. When several match, it returns the first in natural order; with no match it returns null. It skips cursor iteration, making it ideal for lookups by a unique field such as _id or username.

CategoryRead
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.<collection>.findOne(<filter>, <projection>)

⚙️ Parameters

filterPredicate object; supports query operators.
projectionOptional. Controls returned fields: 1 include / 0 exclude.

Returns:The first matching document; null when nothing matches.

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// findOne: look up a user by _id
const user = db.users.findOne(
  { _id: ObjectId('64c8a1b20000000000000001') },
  { name: 1, age: 1, _id: 0 }
)
user

Output:

{ name: 'Alice', age: 21 }

📌 Notes

📚 Related Tutorials

❓ FAQ

QIs findOne the same as find().limit(1)?
ASimilar, but findOne() returns the document directly while find() returns a cursor to iterate; prefer findOne() for unique-field lookups.
QWhat does findOne return when nothing is found?
AIt returns null without throwing; typically consumed as if (doc) { ... }.
QCan findOne return only some fields?
AYes — pass a projection as the second argument, e.g. {name: 1}.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏