Defending champion Novak Djokovic was made to battle before beating Gael Monfils 6-7, 6-2, 6-4 in the quarter-finals of the Shangai Masters.