NO₂ is a polyatomic ion called a nitrite. It's 'brother', NO₃, is called nitrate. When writing the formula for compounds when the name is given, check out the ending of the 'second name'. If the compound's name ends in -'ide', it's probably a binary compound (one containing only two kinds of elements). There are exceptions to this like a compound containing the hydroxide (OH⁻) polyatomic ion. The name of most polyatomic ions formed with a non-metal combined with oxygen end in -ite, or -ate. Examples, in addition to the nitrite and nitrate are: carbonate (CO₃) hydrogen carbonate, HCO₃, sulfite (SO₃), sulfate (SO₄).
Magnesium nitride is Mg₂N₃. The -ide ending tells us its binary and therefore contains only Mg⁺² and N⁻³. Balancing the charges requires 3 Mg's and 2 N's.