如何使用Node.js在Mongoose中加入2个集合

时间:2019-07-16 08:26:02

标签: node.js mongoose-schema

我想使用猫鼬nodejs加入两个集合,但是我被困住了,

collection1

collection2

const mongoose = require('mongoose');
const gameSchema = new mongoose.Schema({

    providerName:{
        type: String
    },
    gamesSettings :[{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'games_setting'
    }]
});

module.exports = mongoose.model('gamesDetails', gameSchema);

This is the route : 

router.get('/', async (req, res)=>{


    try {
        const gamesDetails1 = await joinCollection.find();
        res.json(gamesDetails1);
        //res.render('./games/gamesetting', { data: gamesDetails1 });
    } catch (e) {
        res.json({ message: e });
    }

});

我得到的回应是空的。

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题,但是我认为您需要执行的是填充gameeSetting的查询。答案是:

const details = await gamesDetails.find().populate('gamesSettings');