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

227
Visualizações
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 Respostas
Responde à pergunta

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 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