Real Madrid survived an early scare to comfortably see off Legia Warsaw 5-1 in the Champions League and extend their unbeaten run to 23 games across all competitions.