CRUD Basics: Inserting and Finding Documents
📝 CRUD Basics: Inserting and Finding Documents
Inserting Documents
insertOne adds a single document; insertMany adds several at once. If you don't supply your own _id, MongoDB generates one automatically.
insertOne
insertMany
Finding Documents: find and findOne
find() matches potentially many documents; findOne() returns just the first match, or null if nothing matches.
Basic Queries
Query Operators: Filtering Beyond Exact Match
A plain field/value pair means "equals" implicitly. MongoDB's query operators (always prefixed with $) express everything else:
| Operator | Meaning |
|---|---|
$eq | Equals (usually left implicit) |
$ne | Not equal |
$gt / $gte | Greater than / greater than or equal |
$lt / $lte | Less than / less than or equal |
$in | Matches any value in a given list |
$nin | Matches none of the values in a given list |
Combining Comparison Operators
Using $in
Projection: Choosing Which Fields Come Back
A second argument to find()/findOne() controls which fields are returned — 1 to include, 0 to exclude (a document's _id is included by default unless explicitly excluded). This is conceptually similar to the client-specifies-what-it-wants idea the GraphQL course covers at the whole-API level — here it's just scoped to a single query:
A Projected Query
Combining Conditions: Implicit AND, and $or
Multiple fields inside one query object are implicitly ANDed together. For OR logic, use the explicit $or operator with an array of alternative conditions:
Implicit AND vs. Explicit $or
💻 Coding Challenges
Challenge 1: Insert Three Documents at Once
Write an insertMany call adding three "task" documents to a tasks collection, each with a title and a done boolean field.
Goal: Practice the insertMany syntax with a realistic multi-document array.
Challenge 2: Query with Comparison Operators
Write a find query on a products collection returning products priced 20 or more, in either the "electronics" or "home" category.
Goal: Practice combining $gte and $in in one query.
Challenge 3: Write a Projected Query
Write a query on a users collection that returns only each user's email field (no _id, no other fields).
Goal: Practice projection syntax for both inclusion and explicit _id exclusion.
In the interactive mongosh shell, running db.products.find({}) appears to just print a list of documents — which makes it easy to assume find() hands back a plain array. It doesn't: it returns a cursor, an object that lazily fetches matching documents in batches as you iterate it. The shell happens to auto-iterate and print the first batch for convenience, quietly hiding this detail. It matters once you start writing real driver code (Chapter 8), where you'll typically need to explicitly iterate the cursor or call something like .toArray() to get an actual array of documents — code that treats a raw find() result as if it were already an array will not behave the way the shell's output made it look.
🎯 What's Next
The next chapter covers the other half of CRUD: Updating and Deleting Documents — updateOne/updateMany, update operators like $set/$inc/$push, deleteOne/deleteMany, and upserts.