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

183
Visualizações
Find record in mongo $nin on array

I have a database (mongo) that stores posts and user separate. Within the posts schema I have an array. When a user marks a post as read, their user id is pushed into the array on the post schema. I'm then trying to display only the posts which their user id don't appear in the array.

Here is an example of my post structure.

{ "_id" : ObjectId("620eb60406b710c3ba2250f1"), "icon" : "fas fa-bullhorn", "title" : "Test Post", "scope" : true, "postedby" : "System", "postbody" : "Testing only content", "postcolour" : "orange", "posteddate" : "17/02/2022", "posthideuser" : [ "6205612355b2676a978b172b", "6203e917055a3558ffeb9fca" ] "__v" : 0 }

I tried using the following:

const posts = await Post.find({ posthideuser: { $nin: [user_id] } });

Which hasn't worked.

My Application is node, express/ejs. I will be happy to provide more information if required. Thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your query seems to work as expected:

 db.collection.find({
  posthideuser: {
    $nin: [
    "test2"
    ]
  }
})

playground

about 4 years ago · Juan Pablo Isaza Relatório

0

There was an issue with the structure as indicated.

await Post.findByIdAndUpdate(id, { $push: { posthideuser: [res.locals.user_id] } });

should be: await Post.findByIdAndUpdate(id, { $push: { posthideuser: res.locals.user_id } });

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