Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Is there a way to search through embedded arrays using aggregate pipeline in MongoDB?

I need to search through all the arrays within the categories field to find if the product belongs in the 'T-Shirt' category, is there a way to search through all the arrays within the category field?

Any help would be appreciated.

Here is what I have tried so far

// Aggregate Pipeline
db.products.aggregate([
  {$group :
    {_id :
      {productID : '$productID', productTitle : '$productTitle ', categories: '$categories'
    }
  }
},
  {$project :
      {
          category_tshirts : {$in : ['T-Shirts', '$_id.categories']}
      }
   }
]);

Here is an example of one of the documents:

// Example document
{
        "productID" : "B000072SJ2",
        "productTitle" : "Loosegear Short Sleeve",
        "categories" : [ 
            [ 
                "Sports & Outdoors", 
                "Clothing", 
                "Men", 
                "Shirts", 
                "T-Shirts"
            ], 
            [ 
                "Clothing, Shoes & Jewelry", 
                "Men", 
                "Clothing", 
                "Shirts"
            ], 
            [ 
                "Clothing, Shoes & Jewelry", 
                "Men", 
                "Big & Tall"
            ]
        ]
    }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Sure. You may use the unwind operator in order to get rid of nested arrays.

This operator allows you to have as many separate documents as items in the selected array. For example, having this document:

{
  "productID" : "B00006I551",
  "productTitle" : "CASIO F91W-1 Casual Sport Watch",
  "categories" : [ 
     [ 
       "Sports & Outdoors", 
       "Accessories", 
       "Sport Watches"
     ], 
     [ 
       "Clothing, Shoes & Jewelry", 
       "Sport Watches"
     ], 
     [ 
        "Clothing, Shoes & Jewelry", 
        "Men"
     ]
   ]
 }

If you use unwind on the categories field:

db.products.aggregate( [ { $unwind : "$categories" } ] )

You will get 3 almost similar documents, where only categories are different (1 doc for each item in top level array):

{
  "productID" : "B00006I551",
  "productTitle" : "CASIO F91W-1 Casual Sport Watch",
  "categories" : [ 
       "Sports & Outdoors", 
       "Accessories", 
       "Sport Watches"
     ]
 },
{
  "productID" : "B00006I551",
  "productTitle" : "CASIO F91W-1 Casual Sport Watch",
  "categories" : [ 
       "Clothing, Shoes & Jewelry", 
       "Sport Watches"
     ]
 },
{
  "productID" : "B00006I551",
  "productTitle" : "CASIO F91W-1 Casual Sport Watch",
  "categories" : [ 
        "Clothing, Shoes & Jewelry", 
        "Men"
     ]
 }

Now you may use the $in operator or other ways for querying and filtering via categories.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!