Mongodb Populate is not populating document in Node.js


I am using typegoose and my Query and QueryRule models are as below.

export class Query {
  queryRule: Ref<QueryRule>;
export class QueryRule {
  condition: Condition;

  rules: Ref<QueryRule | ChildRule>[];

I am using the following query to populate.

await QueryModel.findById(queryId).populate('queryRule');

My Query Document is as below.


My QueryRule Document is as below.


But when I access condition and rules of QueryRule using populate query. I am getting undefined even though values exist in that document.

What am I doing wrong here?


I needed 2 things for this.

  1. First is I was missing { ref: () => QueryRule }
  2. Then in populate I had to pass model like this { path : 'queryRule', model: QueryRuleModel }
export class Query {
  @prop({ ref: () => QueryRule })
  queryRule: Ref<QueryRule>;
let query: Query = await QueryModel
      .populate({ path : 'queryRule', model: QueryRuleModel });

Answered By – Anonymous Creator

Answer Checked By – Pedro (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.