Logical query operators

Logical query operators return data based on specified query expressions that are either true or false.

OperatorDescription
$andJoins all query expressions with a logical AND operator
$orJoins all query expressions with a logical OR operator
$notReturns all documents that do NOT match a query expression
$norReturns all documents that do not match any of the query expressions

For the examples in this section, insert the following documents into the catalog collection:

  1. db.catalog.insertMany([
  2. {
  3. product: 'bottle',
  4. price: 15,
  5. stock: 1,
  6. discount: true,
  7. variation: [
  8. {
  9. size: ['small', 'medium', 'large']
  10. },
  11. {
  12. color: ['black', 'silver']
  13. }
  14. ]
  15. },
  16. {
  17. product: 'spoon',
  18. price: 500,
  19. stock: 0,
  20. discount: true,
  21. variation: [
  22. {
  23. size: ['small', 'medium', 'large']
  24. },
  25. {
  26. color: ['silver', 'white']
  27. }
  28. ]
  29. },
  30. {
  31. product: 'cup',
  32. price: 100,
  33. stock: 14,
  34. discount: true,
  35. variation: [
  36. {
  37. size: ['small', 'medium', 'large']
  38. },
  39. {
  40. color: ['red', 'black', 'white']
  41. }
  42. ]
  43. },
  44. {
  45. product: 'bowl',
  46. price: 56,
  47. stock: 5,
  48. discount: false,
  49. variation: [
  50. {
  51. size: ['small', 'medium', 'large']
  52. },
  53. {
  54. color: ['pink', 'white', 'red']
  55. }
  56. ]
  57. }
  58. ])

$and

Syntax: { $and: [ { <condition1> }, { <condition2> } , ... , { <condition3> } ] }

To satisfy more than one query condition when selecting documents, use the $and operator.

Example: Select documents that satisfy both of these expressions in the catalog collection:

  • price field is less than 100 AND
  • stock field is not 0
  1. db.catalog.find({
  2. $and: [
  3. {
  4. price: {
  5. $lt: 100
  6. }
  7. },
  8. {
  9. stock: {
  10. $ne: 0
  11. }
  12. }
  13. ]
  14. })

The output:

  1. [
  2. {
  3. _id: ObjectId("639ba4a0071b6bed396a8f13"),
  4. product: 'bottle',
  5. price: 15,
  6. stock: 1,
  7. discount: true,
  8. variation: [
  9. { size: ['small', 'medium', 'large'] },
  10. { color: ['black', 'silver'] }
  11. ]
  12. },
  13. {
  14. _id: ObjectId("639ba4a0071b6bed396a8f16"),
  15. product: 'bowl',
  16. price: 56,
  17. stock: 5,
  18. discount: false,
  19. variation: [
  20. { size: ['small', 'medium', 'large'] },
  21. { color: ['pink', 'white', 'red'] }
  22. ]
  23. }
  24. ]

$or

Syntax: { $or: [ { <condition1> }, { <condition2> } , ... , { <conditionN> } ] }

To satisfy either one or more conditions in a query, use the $or operator to join the conditions.

Example: Select the documents that match these expressions:

  • discount field is true and stock field is not 0 OR
  • price field is less than or equal to 60
  1. db.catalog.find({
  2. $or: [
  3. {
  4. $and: [
  5. {
  6. discount: true
  7. },
  8. {
  9. stock: {
  10. $ne: 0
  11. }
  12. }
  13. ]
  14. },
  15. {
  16. price: {
  17. $lte: 60
  18. }
  19. }
  20. ]
  21. })

The output:

  1. [
  2. {
  3. _id: ObjectId("639ba4a0071b6bed396a8f13"),
  4. product: 'bottle',
  5. price: 15,
  6. stock: 1,
  7. discount: true,
  8. variation: [
  9. { size: ['small', 'medium', 'large'] },
  10. { color: ['black', 'silver'] }
  11. ]
  12. },
  13. {
  14. _id: ObjectId("639ba4a0071b6bed396a8f15"),
  15. product: 'cup',
  16. price: 100,
  17. stock: 14,
  18. discount: true,
  19. variation: [
  20. { size: ['small', 'medium', 'large'] },
  21. { color: ['red', 'black', 'white'] }
  22. ]
  23. },
  24. {
  25. _id: ObjectId("639ba4a0071b6bed396a8f16"),
  26. product: 'bowl',
  27. price: 56,
  28. stock: 5,
  29. discount: false,
  30. variation: [
  31. { size: ['small', 'medium', 'large'] },
  32. { color: ['pink', 'white', 'red'] }
  33. ]
  34. }
  35. ]

$not

Syntax: { <field>: { $not: { <condition> } } }

To select documents that fail to match a particular query condition, use the $not operator.

Example: The following operation selects documents that do not satisfy the specified expression, where the stock field is not less than 5.

  1. db.catalog.find({
  2. stock: {
  3. $not: {
  4. $lt: 5
  5. }
  6. }
  7. })

The output:

  1. [
  2. {
  3. _id: ObjectId("639ba4a0071b6bed396a8f15"),
  4. product: 'cup',
  5. price: 100,
  6. stock: 14,
  7. discount: true,
  8. variation: [
  9. { size: ['small', 'medium', 'large'] },
  10. { color: ['red', 'black', 'white'] }
  11. ]
  12. },
  13. {
  14. _id: ObjectId("639ba4a0071b6bed396a8f16"),
  15. product: 'bowl',
  16. price: 56,
  17. stock: 5,
  18. discount: false,
  19. variation: [
  20. { size: ['small', 'medium', 'large'] },
  21. { color: ['pink', 'white', 'red'] }
  22. ]
  23. }
  24. ]

$nor

Syntax: { $nor: [ { <condition1> }, { <condition2> }, ... { <conditionN> } ] }

To select documents that fail to match any of the conditions in a specified query, use the $nor operator.

Example: Select the documents that fail to match any of these conditions:

  • discount field is true and stock field is not 0
  • price field is less than or equal to 60
  1. db.catalog.find({
  2. $nor: [
  3. {
  4. $and: [
  5. {
  6. discount: true
  7. },
  8. {
  9. stock: {
  10. $ne: 0
  11. }
  12. }
  13. ]
  14. },
  15. {
  16. price: {
  17. $lte: 60
  18. }
  19. }
  20. ]
  21. })

The output:

  1. [
  2. {
  3. _id: ObjectId("639ba4a0071b6bed396a8f14"),
  4. product: 'spoon',
  5. price: 500,
  6. stock: 0,
  7. discount: true,
  8. variation: [
  9. { size: ['small', 'medium', 'large'] },
  10. { color: ['silver', 'white'] }
  11. ]
  12. }
  13. ]