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

344
Visualizações
Query MongoDB nested Array of arrays for based on multiple values
{
  'id' : 1,
  'properties':{
    'type': [[0.05, 'education'],[0.6, 'health'],[0.9, 'math']]
  }
},
{
  'id' : 1,
  'properties':{
    'type': [[0.05, 'education'],[0.01, 'health'],[0.9, 'math']]
  }
},

How could you query based on two value parameters in nested array of arrays? Goal would be to return the 1st document with properties.type[0] > 0.5, and properties.type[1] == 'health'.

Have tried the following query with no luck.

db.collection.find({'properties.type': {'$elemMatch':{'$elemMatch':{'$all': [{ "$gt": 0.5, "$lt": 1 },'health']}}}})
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Here please try this, I use $unwind and $arrayElemAt.

db.collection.aggregate([
  {
    $unwind: "$properties.type"
  },
  {
    $match: {
      "$and": [
        {
          "$expr": {
            "$gt": [
              {
                $arrayElemAt: [
                  "$properties.type",
                  0
                ]
              },
              0.5
            ]
          }
        },
        {
          "$expr": {
            "$eq": [
              {
                $arrayElemAt: [
                  "$properties.type",
                  1
                ]
              },
              "health"
            ]
          }
        }
      ],
      
    }
  },
  {
    $limit: 1
  }
])

Updated

db.collection.aggregate([
  {
    "$addFields": {
      "orignalObj": "$$ROOT"
    }
  },
  {
    $unwind: "$properties.type"
  },
  {
    $match: {
      "$and": [
        {
          "$expr": {
            "$gt": [
              {
                $arrayElemAt: [
                  "$properties.type",
                  0
                ]
              },
              0.5
            ]
          }
        },
        {
          "$expr": {
            "$eq": [
              {
                $arrayElemAt: [
                  "$properties.type",
                  1
                ]
              },
              "health"
            ]
          }
        }
      ],
      
    }
  },
  {
    $limit: 1
  },
  {
    $replaceRoot: {
      newRoot: "$orignalObj"
    }
  }
])
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