MongoDB

Stage 2 · Lesson 3 of 9

Comparison and logical query operators

Reading and writing documents

start with $ and go inside a 's value. Comparison operators are $eq, $ne, $gt, $gte, $lt, $lte, $in and $nin, as in { price: { $gte: 10, $lt: 50 } }. Logical operators $and, $or, $nor and $not combine conditions.

$exists tests whether a field is present, $type tests its BSON type and $regex matches text patterns. Several conditions on different fields are already ANDed, so you need an explicit $and mainly when two conditions share a key. $ne and $nin also match documents where the field is missing, which surprises people.

Common trapwriting a JavaScript object with the same key twice, such as { price: { $gt: 10 }, price: { $lt: 50 } }: the second silently replaces the first.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with

  • CourseFree
    MongoDB University

    MongoDB

    Free: the CRUD lessons on inserting, finding, replacing, updating and deleting documents, with labs

Practise

  • Test filters and updates on your own sample documents and share the link when you get stuck

Keep handy

Mark this stage as not done?