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

111
Vistas
How to exclude array of ids from query

I have an array of _ids and I want to exclude them from the query:

console.log('relatedCards', relatedCards);
const motherCards = await db.MotherCard.find({ "_id": { $ne : relatedCards } });

But it returns this error:

no

Why this is happening and how to fix this ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

$ ne is usually used for a value. But if you want to exclude multiple items in array, you can use $ nin

For example I have this model:

{
  "_id": "5a934e000102030405000000",
  "key": 1
},
{
  "_id": "5a934e000102030405000001",
  "key": 2
},
{
  "_id": "5a934e000102030405000002",
  "key": 3
},
{
  "_id": "5a934e000102030405000003",
  "key": 4
},
{
  "_id": "5a934e000102030405000004",
  "key": 5
}

And I want exclude key:3 and key5:

db.collection.find({
key: {
  "$nin": [
    3,
    5
  ]
}
})

Now this query return a correct result:

[
  {
    "_id": "5a934e000102030405000000",
    "key": 1
  },
  {
    "_id": "5a934e000102030405000001",
    "key": 2
  },
  {
    "_id": "5a934e000102030405000003",
    "key": 4
  }
]

I hope this solution helps you.

about 4 years ago · Juan Pablo Isaza 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