I think what you mean is Mg(OH)2 vs MgO2H2. This is an ionic compound because it has a metal (magnesium). In it, Mg+2 is the cation (positive ion) and OH- is the anion. Because an ionic compound must have a zero net charge, there needs to be two OH- ions to cancel the +2 charge of the magnesium. so in the first formula it is shown that there are two OH- ions, but the second one is incorrect because OH is one ion and the elements are not separately written like that in an ionic compounds formula.
hope this helps