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

141
Visualizações
Mongodb, query within key-pair values

Is there any way to query inside this struture by the object keys of roles?

   interface UsersModel{
         _id: string;
         email: string;
         roles: { [key: string]: RolesType[] };
    }

For now I get the data into users like:

roles: {
   $ne: null
}

and then

const finalResult = users.filter(u => Object.keys(u.roles).includes('objectKeyIwant'));

Can this be done on the mongodb side and avoid getting all the data then filter it?

Data examples: The user collection is like this:

{
    _id: '610baff85c071a2bd59dc84f',
    email: 'some@email.com',
    roles : {
        "shop1" : [ 
            "shop_stock_manager"
        ],
        "shop2" : [ 
            "shop_admin", 
            "shop_stock_manager"
        ]
    }
}

And I want to get all the users that have any role in shop2.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

use this

db.collection.find({"roles.shop2":{$ne:[]}})
about 4 years ago · Juan Pablo Isaza 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