For sodium chloride, there are 6 anions around each cation (coordination number of Na=6) because of the 1:1 stoichiometry, there must also be 6 cations around each Cl− ion.
Similarly, Mg2+O−2 also has the octahedral structure and follows NaCl type geometry. Therefore, the co-ordination number of Mg is also 6.