Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda