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

144
Vistas
Query builder how to use "where" clause on populated documents

I'm having some issues using the Mongoose queries. I'm just trying to find documents in a collection using the where clause.

Unfortunately, it seems you can't use the where clause on populated documents.


This is the collection schema

const schema: Schema = new Schema({
    game: { type: Schema.Types.ObjectId, ref: 'Game', required: true, index: true },
    players: [{ type: Schema.Types.ObjectId, ref: 'Player', required: true, index: true }],
    scores: [{ type: String }],
    resultDate: { type: Date}
});

I'm trying to find all games where type = 1v1, so I tried this

let query = Matchs.find()
    .populate('game')
    .populate('players')
    .where('game.name').equals('Trackmania')

const matchs: Match[] = await query.exec();

This returns an empty array.


Notes

  • Removing the where clause returns the correct results (all Matchs)
  • Any where clause on game returns an empty array
  • I'd like to use query builder instead of passing a json because I use some parameters to define what I am querying

I read that where clause didn't work on nested documents, but there must be a way to do this right ? Am I missing something ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can Use Lookup and Match instead of Where.

MatchesModel.aggregate([

const GameName =  req.query;

  {"$lookup":{
    "from":"games", // name of the foreign collection
    "localField":"game",
    "foreignField":"_id",
    "as":"game"
  }},
   {"$lookup":{
    "from":"players", 
    "localField":"players",
    "foreignField":"_id",
    "as":"players"
  }},
  {"$match":{
     "game.name":{
      "$eq": GameName
    }
  }}
])

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