DEV Community

Perm Chao
Perm Chao

Posted on

3 2

NestJS with mongoose populate()

ตัว mongoose เองมี method populate() ที่ช่วยในการ $lookup อยู่แล้ว โดยเราสามารถใช้งานได้จากตัวของ model


// declare
constructor(
    @Inject(firstModelProviders[0].provide) private firstModel: Model<FirstModelDocument>,
    @Inject(secondModelProviders[0].provide) private secondModel: Model<SecondModelDocument>,
  ) {}

// use
const firstModelRepo = await this.firstModel.find().populate('secondModelForeignKey', '', this.secondModel).exec();
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
zodar93 profile image
Tarik MERABET

firstModelProviders[0].provide ???

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay