Array query operators

Array query operators allow you to search for specific elements within an array field in a document.

OperatorDescription
$allSelects an array that contains all elements from a given query.
$elemMatchMatches a document that contains an array field with at least one element that matches all the specified query criteria
$sizeMatches an array with a specified number of elements

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

  1. db.team.insertMany([
  2. {
  3. id: 1,
  4. name: 'Jack Smith',
  5. position: 'Manager',
  6. skills: ['leadership', 'communication', 'project management'],
  7. contact: {
  8. email: 'john@example.com',
  9. phone: '123-456-7890'
  10. },
  11. active: true
  12. },
  13. {
  14. id: 2,
  15. name: 'Jane Mark',
  16. position: 'Software Developer',
  17. skills: ['Java', 'Python', 'C++'],
  18. contact: {
  19. email: 'jane@example.com',
  20. phone: '123-456-7891'
  21. },
  22. active: false
  23. },
  24. {
  25. id: 3,
  26. name: 'Bob Johnson',
  27. position: 'Graphic Designer',
  28. skills: ['Adobe Photoshop', 'Illustrator', 'InDesign'],
  29. contact: {
  30. email: 'bob@example.com',
  31. phone: '123-456-7892'
  32. },
  33. active: true
  34. },
  35. {
  36. id: 4,
  37. name: 'Alice Williams',
  38. position: 'Marketing Coordinator',
  39. skills: ['communication', 'content creation', 'event planning'],
  40. contact: {
  41. email: 'alice@example.com',
  42. phone: '123-456-7893'
  43. },
  44. active: true
  45. }
  46. ])

$all

Syntax: { <field>: { $all: [ <element1>, <element2>, ... <elementN> ] } }

Use the $all operator when you want to select documents that contain every single element in a specified array.

Array query operators - 图1note

When using an $all operator, the order of the elements and array size does not matter, as long as the array contains all the elements in the query.

Example: Find all documents in the team collection where the skills field contains both communication and content creation as elements using the following query operation:

  1. db.team.find({
  2. skills: {
  3. $all: ['communication', 'content creation']
  4. }
  5. })

The output:

  1. [
  2. {
  3. _id: ObjectId("63a5bb4acf72d6203bb45bb5"),
  4. id: 4,
  5. name: 'Alice Williams',
  6. position: 'Marketing Coordinator',
  7. skills: ['communication', 'content creation', 'event planning'],
  8. contact: { email: 'alice@example.com', phone: '123-456-7893' },
  9. active: true
  10. }
  11. ]

$elemMatch

Syntax: { <field>: { $elemMatch: { <condition1>, <condition2>, ... <conditionN>} } }

To select documents in a specified array field where one or more elements match all listed query conditions, use the $elemMatch operator.

Example: Find documents in the team collection where the skills field is an array that contains the element “Java”, and array does not contain the element communication. Use the following query operation:

  1. db.team.find({
  2. skills: {
  3. $elemMatch: {
  4. $eq: 'Java',
  5. $nin: ['communication']
  6. }
  7. }
  8. })

The output:

  1. [
  2. {
  3. _id: ObjectId("63aa247e69c82de72bd40b93"),
  4. id: 2,
  5. name: 'Jane Mark',
  6. position: 'Software Developer',
  7. skills: ['Java', 'Python', 'C++'],
  8. contact: { email: 'jane@example.com', phone: '123-456-7891' },
  9. active: false
  10. }
  11. ]

$size

Syntax: { <field>: { $size: <number-of-elements> } }

The $size operator is ideal for selecting array fields containing a specified number of elements.

Example: Select the documents in the team collection where the skills array contains only three elements.

  1. db.team.find({
  2. skills: {
  3. $size: 3
  4. }
  5. })

The output:

  1. [
  2. {
  3. _id: ObjectId("63aa247e69c82de72bd40b92"),
  4. id: 1,
  5. name: 'Jack Smith',
  6. position: 'Manager',
  7. skills: ['leadership', 'communication', 'project management'],
  8. contact: { email: 'john@example.com', phone: '123-456-7890' },
  9. active: true
  10. },
  11. {
  12. _id: ObjectId("63aa247e69c82de72bd40b93"),
  13. id: 2,
  14. name: 'Jane Mark',
  15. position: 'Software Developer',
  16. skills: ['Java', 'Python', 'C++'],
  17. contact: { email: 'jane@example.com', phone: '123-456-7891' },
  18. active: false
  19. },
  20. {
  21. _id: ObjectId("63aa247e69c82de72bd40b94"),
  22. id: 3,
  23. name: 'Bob Johnson',
  24. position: 'Graphic Designer',
  25. skills: ['Adobe Photoshop', 'Illustrator', 'InDesign'],
  26. contact: { email: 'bob@example.com', phone: '123-456-7892' },
  27. active: true
  28. },
  29. {
  30. _id: ObjectId("63aa247e69c82de72bd40b95"),
  31. id: 4,
  32. name: 'Alice Williams',
  33. position: 'Marketing Coordinator',
  34. skills: ['communication', 'content creation', 'event planning'],
  35. contact: { email: 'alice@example.com', phone: '123-456-7893' },
  36. active: true
  37. }
  38. ]