How can I use one _id between two collections when joining two collection How can I use one _id between two collections when joining two collection mongoose mongoose

How can I use one _id between two collections when joining two collection


If you are using mongoose, it has a populate method built in. Consider changing your Word schema to:

const WordSchema = new Schema({    word:{        type:String,    },    translate: {        type:String,    },    kind:{        type:String,    },    exampleSentence: {        type:String,         },    isCorrect: {        type:Boolean    },    event: {        type:mongoose.Schema.ObjectId,        ref: 'Test',        required:true    }});

Then, instead of using lookup, do (where Word is a reference to the WordSchema):

router.get("/", async (req,res) => {    const data = await Word.find().populate('Test').exec();    res.json(data);}) 

Populate will automatically populate your references for you.