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

204
Visualizações
Query child model with mongoose

I'm updating the mongoose version from 5 to 6 and found a change in the behavior.

This is a small example: We have two models

const MessageSchema = new mongoose.Schema({ text: String });
const Message = mongoose.model('Message', MessageSchema);

const Notification = Message.discriminator(
    'Notification',
    new mongoose.Schema({important: Boolean})
);

And let's say, we create two documents:

const m = new Message({text: 'Some text 1'});
await m.save();

const n = new Notification({important: true, text: 'Some text'});
await n.save();

And we run this query:

await Message.find({important: true})

In mongoose v5 the query will use important in spite of it not being declared in the Message schema. And the result will be just one document.

However, in the new version, it will ignore important if you query Messages, and will return all the documents, because without important query is just {}.

My question is, how can I achieve the same behavior with the new version?

I know that I could use the Notification model instead of Messages, but image if there were several models like Notification (children of Message), that had important in them, and I wanted to search in all of them.

over 4 years ago · Santiago Trujillo
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