sql >> Base de Datos >  >> NoSQL >> MongoDB

mongodb $relajarse para un documento anidado no ideal

  • $unwind deconstruye la matriz de rondas
  • $project para mostrar los campos obligatorios
db.collection.aggregate([
  { $unwind: "$rounds" },
  {
    $project: {
      GameID: 1,
      ComputerName: 1,
      max_player_pot: "$rounds.round_values.max_player_pot",
      pot_multiple: "$rounds.round_values.pot_multiple"
    }
  }
])

Patio de juegos

Un enfoque más dinámico,

  • $mergeObjects para fusionar los campos obligatorios desde la raíz y round_values objeto
  • $replaceRoot para reemplazar el objeto fusionado anterior a la raíz
db.collection.aggregate([
  { $unwind: "$rounds" },
  {
    $replaceRoot: {
      newRoot: {
        $mergeObjects: [
          {
            GameID: "$GameID",
            ComputerName: "$ComputerName"
          },
          "$rounds.round_values"
        ]
      }
    }
  }
])

Patio de juegos