MongoDB Query Operators – Simple Scriptum

This short scriptum explains how to write simple MongoDB queries using common comparison, logical, and array operators.

In the examples below, assume we are working with a collection called students.

Example document:

{
  "name": "Anna",
  "age": 17,
  "grade": 2,
  "city": "Vienna",
  "points": 84,
  "tags": ["javascript", "mongodb"],
  "projects": [
    { "title": "API Server", "score": 91, "team": true },
    { "title": "Portfolio", "score": 78, "team": false }
  ]
}

A simple MongoDB query usually looks like this:

db.students.find({ field: condition })

1. Comparison Operators

$eq

Matches values equal to a specified value.

db.students.find({ city: { $eq: "Vienna" } })

This finds all students whose city is exactly "Vienna".

$gt

Matches values greater than a specified value.

db.students.find({ age: { $gt: 16 } })

This finds all students older than 16.

$gte

Matches values greater than or equal to a specified value.

db.students.find({ points: { $gte: 80 } })

This finds all students with 80 points or more.

$in

Matches any values specified in an array.

db.students.find({ city: { $in: ["Vienna", "Graz"] } })

This finds all students from Vienna or Graz.

$lt

Matches values less than a specified value.

db.students.find({ age: { $lt: 18 } })

This finds all students younger than 18.

$lte

Matches values less than or equal to a specified value.

db.students.find({ points: { $lte: 60 } })

This finds all students with 60 points or fewer.

$ne

Matches all values not equal to a specified value.

db.students.find({ city: { $ne: "Vienna" } })

This finds all students not living in Vienna.

$nin

Matches if the value is not equal to any of a given list of values.

db.students.find({ city: { $nin: ["Vienna", "Graz"] } })

This finds all students whose city is neither Vienna nor Graz.


2. Logical Operators

$and

Joins query clauses with a logical AND and returns documents that match the conditions of all clauses.

db.students.find({
  $and: [
    { age: { $gte: 16 } },
    { points: { $gte: 80 } }
  ]
})

This finds students who are at least 16 years old and have at least 80 points.

$nor

Joins query clauses with a logical NOR and returns all documents that fail to match all clauses.

db.students.find({
  $nor: [
    { city: "Vienna" },
    { points: { $lt: 50 } }
  ]
})

This finds students who are not from Vienna and also do not have fewer than 50 points.

$not

Inverts the effect of a query predicate and returns documents that do not match the query predicate.

db.students.find({ points: { $not: { $gt: 90 } } })

This finds students whose points are not greater than 90.

$or

Joins query clauses with a logical OR and returns all documents that match at least one clause.

db.students.find({
  $or: [
    { city: "Vienna" },
    { city: "Linz" }
  ]
})

This finds students who live in Vienna or Linz.


3. Array Operators

$all

Matches arrays that contain all elements specified in the query.

db.students.find({ tags: { $all: ["javascript", "mongodb"] } })

This finds students whose tags array contains both "javascript" and "mongodb".

$elemMatch

Selects documents if at least one element in the array field matches all the specified $elemMatch conditions.

db.students.find({
  projects: {
    $elemMatch: {
      score: { $gte: 90 },
      team: true
    }
  }
})

This finds students who have at least one project with a score of 90 or more and where team is true.

$size

Selects documents if the array field contains the specified number of elements.

db.students.find({ tags: { $size: 2 } })

This finds students whose tags array has exactly 2 elements.


4. Notes About Simple Query Writing

Often, MongoDB allows a shorter form for equality:

db.students.find({ city: "Vienna" })

This is equivalent to:

db.students.find({ city: { $eq: "Vienna" } })

Also, conditions on different fields are automatically combined with AND:

db.students.find({ age: { $gte: 16 }, points: { $gte: 80 } })

This is similar to using $and, but shorter.


5. Combining Operators for More Advanced Queries

Here are a few examples showing how operators can be combined.

Example 1: Range Query

Find students whose age is between 16 and 18.

db.students.find({
  age: { $gte: 16, $lte: 18 }
})

Example 2: Logical Combination

Find students who either have at least 90 points or live in Graz, but are not in grade 1.

db.students.find({
  $and: [
    {
      $or: [
        { points: { $gte: 90 } },
        { city: "Graz" }
      ]
    },
    { grade: { $ne: 1 } }
  ]
})

Example 3: Arrays and Conditions Together

Find students who have both javascript and mongodb tags and at least one team project with a score above 85.

db.students.find({
  tags: { $all: ["javascript", "mongodb"] },
  projects: {
    $elemMatch: {
      team: true,
      score: { $gt: 85 }
    }
  }
})

6. Summary

These operators are useful for building many kinds of MongoDB queries:

  • Comparison: $eq, $gt, $gte, $in, $lt, $lte, $ne, $nin
  • Logical: $and, $nor, $not, $or
  • Array: $all, $elemMatch, $size

A good strategy is:

  1. Start with a simple equality query.
  2. Add comparison operators for numeric values.
  3. Use logical operators to combine conditions.
  4. Use array operators when fields contain lists or objects inside arrays.