Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!