Skip to content

Instantly share code, notes, and snippets.

@powertech2nd
Created March 5, 2026 11:45
Show Gist options
  • Select an option

  • Save powertech2nd/220d1b203ed1655576f8dd3c4ae51fca to your computer and use it in GitHub Desktop.

Select an option

Save powertech2nd/220d1b203ed1655576f8dd3c4ae51fca to your computer and use it in GitHub Desktop.

01 β€” Finding Data (The Basics)

Goal: Look up a single specific record, like a user by their email.


Concept Comparison

Feature SQL MongoDB
Concept SELECT * with LIMIT 1 .findOne() method
Returns One row One document (JSON object)

SQL Way

SELECT *
FROM Users
WHERE email = 'jane@example.com'
LIMIT 1;

MongoDB Way

db.users.findOne({ email: "jane@example.com" });

πŸ” Breaking It Down

db         β†’  the database connection
.users     β†’  the collection (like a table)
.findOne() β†’  returns the FIRST match only
{ ... }    β†’  the filter (like WHERE)

πŸ’‘ Notes

  • findOne() returns null if nothing matches β€” not an error.
  • To find multiple documents, use .find() instead (see next section).
  • MongoDB automatically assigns every document a unique _id field.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment