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

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

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