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

233
Vistas
Finding Unviewed Users in mongodb

I currently have a collection of posts. On each post when a user marks it as read it pushes their user _id onto an array inside the the post document.

I'm then trying to read all the posts, and find which users have not read the document. My idea was to try us this $nin function whilst cycling through the posts and then storing them in a new object that I can call with the same index number. However I'm struggling to find the correct syntax to get the it due to object been in string format.

  const allPosts = await Post.find({});
    for (let i = 0; i < allPosts.length; i++) {
    console.log(i)
    const findUsers = await User.find({ _id: { $nin: ObjectID([allPosts[i].posthideuser]) } })
    console.log(findUsers);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are converting the whole array into an ObjectID instead you want to convert individual values. Use .map() with ObjectId constructor

 const allPosts = await Post.find({});
    for (let i = 0; i < allPosts.length; i++) {
    console.log(i)
    const objIds = allPosts[i].posthideuser.map((x) => new mongoose.Types.ObjectId(x));
    const findUsers = await User.find({ _id: { $nin: objIds } })
    console.log(findUsers);
}

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