簡體   English   中英

在 Mongoose 中填充虛擬字段

[英]Populating Virtual Field in Mongoose

我目前正在構建一個記錄撲克統計數據的應用程序。 用戶創建玩家,然后用這些玩家填充游戲。

我有一個游戲 model 引用了來自玩家 model 的玩家:

const gameSchema = new mongoose.Schema({
  firstPlace: { type: mongoose.Schema.ObjectId, ref: 'Players', required: true },
  secondPlace: { type: mongoose.Schema.ObjectId,ref: 'Players', required: true },
  thirdPlace: { type: mongoose.Schema.ObjectId, ref: 'Players', required: true },
  fourthPlace: { type: mongoose.Schema.ObjectId, ref: 'Players' },
  fifthPlace: { type: mongoose.Schema.ObjectId, ref: 'Players' },
  sixthPlace: { type: mongoose.Schema.ObjectId, ref: 'Players' },
  seventhPlace: { type: mongoose.Schema.ObjectId, ref: 'Players' },
  eighthPlace: { type: mongoose.Schema.ObjectId, ref: 'Players' },
  ninthPlace: { type: mongoose.Schema.ObjectId, ref: 'Players' },
  buyIn: { type: Number, required: true },
  firstPrize: { type: Number, required: true },
  secondPrize: { type: Number, required: true },
  thirdPrize: { type: Number, required: true },
  date: { type: String },
  notes: { type: String },
  userId: { type: mongoose.Schema.ObjectId, ref: 'Users', required: true },
})

為了獲取單個游戲的信息,我填充了對玩家的引用 -

// Get single Game
async function getSingleGame(req, res, next) {
  try {
    const { gameId } = req.params
    const foundGame = await Games.findById(gameId)
      .populate('userId')
      .populate('firstPlace')
      .populate('secondPlace')
      .populate('thirdPlace')
      .populate('fourthPlace')
      .populate('fifthPlace')
      .populate('sixthPlace')
      .populate('seventhPlace')
      .populate('eighthPlace')
      .populate('ninthPlace')
    if (!foundGame) throw new NotFound()
    return res.status(200).json(foundGame)
  } catch (err) {
    next(err)
  }
}

這很好用,例如firstPlace填充了包含該播放器所有信息的 object。

但是,我的用戶 model 上有一個虛擬字段,它可以獲取用戶創建的所有游戲。 下面的代碼 -

userSchema
  .virtual('addedGames', {
    ref: 'Games',
    localField: '_id',
    foreignField: 'userId',
  })
  .get(function (addedGames) {
    if (!addedGames) return
    return addedGames.map((game) => {
      return {
        _id: game._id,
        firstPlace: game.firstPlace,
        secondPlace: game.secondPlace,
        thirdPlace: game.thirdPlace,
        fourthPlace: game.fourthPlace,
        fifthPlace: game.fifthPlace,
        sixthPlace: game.sixthPlace,
        seventhPlace: game.seventhPlace,
        eigthPlace: game.eigthPlace,
        ninthPlace: game.ninthPlace,
        buyIn: game.buyIn,
        firstPrize: game.firstPrize,
        secondPrize: game.secondPrize,
        thirdPrize: game.thirdPrize,
      }
    })
  })

這行得通 - 但所有放置都是objectIds而不是對象本身。 我的問題是 - 我怎樣才能在這個虛擬領域填充這些對Players的引用?

在此先感謝,我一直在努力解決這個問題!

經過多次試驗和錯誤,我添加了這個來填充嵌套對象 -

let user = await User.findById(currentUserId)
  .populate('addedPlayers')
  .populate([
    {
      path: 'addedGames',
      populate: {
        path: 'firstPlace secondPlace thirdPlace fourthPlace fifthPlace sixthPlace seventhPlace eighthPlace',
      },
    },
  ])

最初我只是populate('addedGames')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM