Typorithm · medium

Logical & Element

mongo-logicalmongodblogicalelementexistence
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Logical & Element

0%

Query Operators

Logical operators combine conditions: $or matches any, $and matches all, $nor matches none, and $exists checks whether a field is present.

$or accepts an array of clauses and matches a document satisfying at least one of them.

$and is implicit between separate fields, but explicit $and is needed when one field must satisfy two conditions; $nor is the negation of $or.

$exists tests presence: $exists: true keeps documents that have the field, $exists: false keeps those that lack it.

01

$or matches any clause; $and matches every clause.

02

$nor matches documents satisfying none of the clauses.

03

$exists checks field presence, not its value.

db.users.find({ $or: [{ age: { $lt: 18 } }, { vip: true }] })
db.users.find({ $and: [{ active: true }, { age: { $gte: 21 } }] })
db.users.find({ email: { $exists: true } })
db.users.find({ $nor: [{ banned: true }, { deleted: true }] })

Read the implementation

Connect each part of the code to the concept before you type.

1. Match either

$or takes an array of conditions and matches a document satisfying at least one of them.

$or: [{ age: { $lt: 18 } }, { vip: true }]

2. Match both

$and requires every condition in the array to hold — useful when the same field appears twice.

$and: [{ active: true }, { age: { $gte: 21 } }]

3. Field present

$exists: true matches documents that have the field at all, regardless of its value.

{ $exists: true }

4. Match neither

$nor matches documents that satisfy none of the listed conditions — the negation of $or.

$nor: [{ banned: true }, { deleted: true }]

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Logical & Element: Lesson & Practice · Typorithm | ApplyDjinn