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

199
Visualizações
MongoDB query using intersection of 2 lists

I'm working on a project where we use mongodb 4.4 as our database. The problem is I have to make a query that is too complicated using mongo query language.

That's an exemple of the data in the database:

{
    "neededQuantity": 100,
    "auth": {
        "groups": ["group-1"]
    },
    "createTime": "2022-02-03T14:17:25.809704600Z",
},
{
    "neededQuantity": 120,
    "auth": {
        "groups": ["group-3"]
    },
    "createTime": "2022-02-02T14:17:25.809704600Z",
},
{
    "neededQuantity": 150,
    "auth": {
        "groups": ["group-2","group-1"]
    },
    "createTime": "2022-02-01T14:17:25.809704600Z",
}

The query has to find the documents that have at least 1 element of the list auth.groups in common with an another list that is given as input.

If the input list is ["group-1"], the query result should be:

{
    "neededQuantity": 100,
    "auth": {
        "groups": ["group-1"]
    },
    "createTime": "2022-02-03T14:17:25.809704600Z",
},
{
    "neededQuantity": 150,
    "auth": {
        "groups": ["group-2","group-1"]
    },
    "createTime": "2022-02-01T14:17:25.809704600Z",
}

How can I do it?

Thank you very much in advance.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

you can do it with $in

db.collection.find({
    "auth.groups": {$in: ["group-1"]}
})

playground

more detail about $in here

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