</>
Skip to content
MongoDB lessons (11/38)

MongoDB — Query

Logical Operators

OperatorDescription
$andLogical AND
$orLogical OR
$notLogical NOT
$norNeither condition

Examples

// AND
db.users.find({ $and: [{ age: { $gt: 18 } }, { active: true }] })

// OR
db.users.find({ $or: [{ country: "USA" }, { country: "UK" }] })

// NOT
db.users.find({ age: { $not: { $gt: 30 } } })

Array Queries

// Element in array
db.users.find({ hobbies: "reading" })

// All elements
db.users.find({ hobbies: { $all: ["reading", "gaming"] } })

// Array size
db.users.find({ hobbies: { $size: 2 } })

Element Queries

// Exists
db.users.find({ email: { $exists: true } })

// Type
db.users.find({ age: { $type: "int" } })

Regex

db.users.find({ name: /john/i })
db.users.find({ name: /^J/ })

Mini Practice

  1. Use logical operators
  2. Query arrays
  3. Use element queries
  4. Use regex patterns

Up Next

Continue with Update — updating documents.

Related Topics

Frequently Asked Questions about Query

What is Query in MongoDB?

Query is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Query?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Query.

Why is Query important in MongoDB?

Query is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.