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

310
Views
How use the $lookup in mongodb and nodejs because mine is empty

I am looking for a way to search for data that has a foreign key. But I haven't found it yet. I've been stuck here for a day. Here is my model

const mongoose = require('mongoose');

const UserCGN = mongoose.Schema({
   username: String,
   cours: String
})
module.exports = mongoose.model('UserCGN', UserCGN);

const mongoose = require('mongoose');

const Cours = mongoose.Schema({
    name_Cours: String,
    date_Commenc: Date
})

module.exports = mongoose.model('dataCours', Cours);

And I have a data in my database like it

dans UserCGN

username:"njatotianafiononana@gmail.com" cours:"Anglais"

et dans le dataCours

name_Cours:"Anglais" date_Commenc:2022-01-12T00:00:00.000+00:00

Et voici mon code pour récuperer

var coursM = await CoursModel.aggregate( [
    {
      $lookup:
        {
          from: "CGNModel",
          localField: "name_Cours",
          foreignField: "cours",
          as: "inventory_docs"
        }
   }
 ] )
console.log("coursM", coursM);

In my table inventory_docs is always empty. Please, help me

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Are these two models connected in any way? From the snippet you provided, it looks like you forgot to do it, and the model should loke something like this. This way you could do lookup with foreignField: "_id"

const Cours = mongoose.Schema({
    name_Cours: [
      { 
         type: mongoose.Schema.Types.ObjectId,
         ref: 'UserCGN',            
      }
    ],
    date_Commenc: Date
})

about 4 years ago · Juan Pablo Isaza Report

0

Thanks for your help, It is my update in model

const mongoose = require('mongoose');

const Cours = mongoose.Schema({
    name_Cours: [
      { 
         type: mongoose.Schema.Types.ObjectId,
         ref: 'UserCGN',            
      }
    ],
    date_Commenc: Date
})

module.exports = mongoose.model('dataCours', Cours);

And in my $lookup

var coursM = await CoursModel.aggregate( [
    {
      $lookup:
        {
          from: "CGNModel",
          localField: "_id",
          foreignField: "cours",
          as: "inventory_docs"
        }
   }
 ] )

And my data like that in dataCours

id:62ac815300878fa91f1549b9 name_Cours:Array 0:62a33ab745819dea27c8943d date_Commenc:2022-01-12T00:00:00.000+00:00

And in usercng, I have a id

_id:62a33ab745819dea27c8943d

But the result is empty

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!