Goal: Look up a single specific record, like a user by their email.
| Feature | SQL | MongoDB |
|---|---|---|
| Concept | SELECT * with LIMIT 1 |
.findOne() method |
| Returns | One row | One document (JSON object) |
SELECT *
FROM Users
WHERE email = 'jane@example.com'
LIMIT 1;db.users.findOne({ email: "jane@example.com" });db β the database connection
.users β the collection (like a table)
.findOne() β returns the FIRST match only
{ ... } β the filter (like WHERE)
findOne()returnsnullif nothing matches β not an error.- To find multiple documents, use
.find()instead (see next section). - MongoDB automatically assigns every document a unique
_idfield.