Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda