MongoDB: Find documents whoose field value is an array (with examples)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

MongoDB, a NoSQL document-oriented database, supports storing data in flexible, JSON-like documents. In real-world scenarios, data is often not flat and can consist of complex structures such as arrays. Querying documents where a field’s value is an array requires understanding of MongoDB’s array-specific query operators. In this tutorial, we will explore these concepts with hands-on examples.

Prerequisites

  • Basic knowledge of MongoDB
  • MongoDB instance (local or cloud)
  • MongoDB Compass or command line interface to interact with database

Finding Documents with an Array Field

Begin by selecting a collection and then use the find() method to retrieve all documents containing an array field.

db.collection.find({ field_name: { $exists: true, $type: 'array' } });

For example, if we have a collection ‘products’ and we want to find documents where ‘tags’ is an array, we can write:

db.products.find({ tags: { $exists: true, $type: 'array' } });

Querying Specific Values within Arrays

To find documents where an array field contains a certain value, use the array field name directly in the query:

db.collection.find({ 'array_field_name': 'value' });

For instance, to find products tagged as ‘gift’:

db.products.find({ tags: 'gift' });

Matching Whole Array

Use the $all operator to find documents where the array includes all the specified elements, regardless of order.

db.collection.find({
  'array_field_name': {
    $all: ['value1', 'value2']
  }
});

Searching for products containing both ‘gift’ and ‘popular’ tags:

db.products.find({
  tags: {
    $all: ['gift', 'popular']
  }
});

Querying by Array Length

To find documents with an array of a specific length, combine the $size operator with the length you’re interested in:

db.collection.find({ 'array_field_name': { $size: length } });

Let’s find all products with exactly three tags:

db.products.find({ tags: { $size: 3 } });

Using Array Indexes

You can also query by specifying an index in the array:

db.collection.find({ 'array_field_name.index': 'value' });

For example, finding products whose first tag is ‘sale’:

db.products.find({ 'tags.0': 'sale' });

Querying with Dot Notation and $elemMatch

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria:

db.collection.find({
  'array_field_name': {
    $elemMatch: {
      $gte: value1,
      $lte: value2
    }
  }
});

If products have multiple numerical ratings, let’s find those with at least one rating between 4 and 5:

db.products.find({
  ratings: {
    $elemMatch: {
      $gte: 4,
      $lte: 5
    }
  }
});

Advanced Array Queries

For more complex queries, such as retrieving only the matching elements from an array, use the aggregation framework:

db.collection.aggregate([
    { $match: { 'array_field_name': 'value' } },
    { $project: { array_field_name: 1 } },
    { $unwind: '$array_field_name' },
    { $match: { 'array_field_name': 'value' } }
]);

This pipeline will match documents with the ‘value’, unwind the array, and match again to return documents with that specific value in the array field.

Querying Nested Arrays

Since documents can contain arrays of sub-documents or nested arrays, you can also query based on conditions of these complex structures:

db.collection.find({ 'nested.array_field.subfield': 'value' });

For products with a list of specifications, each being an object with ‘key’ and ‘value’, to find products with a specific specification:

db.products.find({
  'specifications': {
    $elemMatch: {
      key: 'Material',
      value: 'Cotton'
    }
  }
});

Conclusion

MongoDB provides a powerful set of query operators for working with arrays, enabling you to handle complex data structures efficiently. Mastery of these operators opens up an extensive range of query capabilities crucial for any MongoDB user.