Skip to content
Advertisement

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"
            ]
        ]
    }

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement