如何使用nodejs加入mongoose中的2个集合
How to join 2 collections in mongoose using nodejs
我想使用 mongoose 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 });
}
});
我的回复为空。
我不确定我是否正确理解了您的问题,但我认为您需要的是在填充 gameeSetting 的位置执行查询。答案是:
const details = await gamesDetails.find().populate('gamesSettings');
我想使用 mongoose 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 });
}
});
我的回复为空。
我不确定我是否正确理解了您的问题,但我认为您需要的是在填充 gameeSetting 的位置执行查询。答案是:
const details = await gamesDetails.find().populate('gamesSettings');