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

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