Spectroscopic study of magnesium dinitrogen and sodium dinitrogen cation