Day 02 Core Concepts

Queries and Operators

Comparison, logical, and array operators. Projection, sort, skip, and limit.

~1 hour Hands-on Precision AI Academy

Today’s Objective

Comparison, logical, and array operators. Projection, sort, skip, and limit.

Queries and Operators is one of the most important topics in MongoDB in 5 Days. This lesson builds the foundation you need before moving to more advanced concepts — take time with each example and run the code yourself.

Query Operators

Comparison Operators
COMPARISON OPERATORS
// Numeric comparisons
db.products.find({ price: { $gt: 100 } }) // > 100
db.products.find({ price: { $gte: 100 } }) // >= 100
db.products.find({ price: { $lt: 50 } }) // < 50
db.products.find({ price: { $ne: 0 } }) // != 0
db.products.find({ category: { $in: ['Electronics', 'Books'] } })
db.products.find({ category: { $nin: ['Food'] } })
Logical Operators
LOGICAL OPERATORS
// AND (implicit — just combine conditions)
db.products.find({ price: { $lt: 100 }, inStock: true })

// Explicit AND
db.products.find({ $and: [ { price: { $lt: 100 } }, { category: 'Electronics' }
]})

// OR
db.products.find({ $or: [ { price: { $lt: 10 } }, { featured: true }
]})

// NOT
db.products.find({ category: { $not: { $eq: 'Food' } } })
Projection, Sort, Limit
PROJECTION, SORT, LIMIT
// Projection: 1 = include, 0 = exclude
db.products.find({}, { name: 1, price: 1, _id: 0 })

// Sort: 1 = ascending, -1 = descending
db.products.find().sort({ price: -1 })

// Pagination
const PAGE = 2;
const PER_PAGE = 10;
db.products.find() .sort({ createdAt: -1 }) .skip((PAGE - 1) * PER_PAGE) .limit(PER_PAGE)
📝 Day 2 Exercise
Build Pagination
  1. Q
  2. u
  3. e
  4. r
  5. y
  6. y
  7. o
  8. u
  9. r
  10. p
  11. r
  12. o
  13. d
  14. u
  15. c
  16. t
  17. s
  18. c
  19. o
  20. l
  21. l
  22. e
  23. c
  24. t
  25. i
  26. o
  27. n
  28. w
  29. i
  30. t
  31. h
  32. :
  33. p
  34. r
  35. i
  36. c
  37. e
  38. r
  39. a
  40. n
  41. g
  42. e
  43. f
  44. i
  45. l
  46. t
  47. e
  48. r
  49. ,
  50. c
  51. a
  52. t
  53. e
  54. g
  55. o
  56. r
  57. y
  58. f
  59. i
  60. l
  61. t
  62. e
  63. r
  64. ,
  65. s
  66. o
  67. r
  68. t
  69. b
  70. y
  71. p
  72. r
  73. i
  74. c
  75. e
  76. a
  77. s
  78. c
  79. e
  80. n
  81. d
  82. i
  83. n
  84. g
  85. ,
  86. a
  87. n
  88. d
  89. p
  90. a
  91. g
  92. i
  93. n
  94. a
  95. t
  96. i
  97. o
  98. n
  99. (
  100. 1
  101. 0
  102. p
  103. e
  104. r
  105. p
  106. a
  107. g
  108. e
  109. )
  110. .
  111. W
  112. r
  113. i
  114. t
  115. e
  116. t
  117. h
  118. e
  119. q
  120. u
  121. e
  122. r
  123. y
  124. f
  125. o
  126. r
  127. p
  128. a
  129. g
  130. e
  131. 1
  132. a
  133. n
  134. d
  135. p
  136. a
  137. g
  138. e
  139. 2
  140. .

Supporting Resources

Go deeper with these references.

MongoDB Docs
Official MongoDB Manual Complete reference documentation for all MongoDB features and operations.
→
MongoDB University
Free MongoDB Courses Official free courses on MongoDB fundamentals, aggregation, and performance.
→
GitHub
MongoDB Driver Examples Official Node.js and Python driver examples on GitHub.
→

Day 2 Checkpoint

Before moving on, make sure you can answer these without looking:

Continue To Day 3
Aggregation Pipelines
→