Actually this is not correct, because not all compounds are molecules. There are also giant structures, both ionic (such as common salt) and covalent, (such as quartz). These compounds are not molecular in nature but are indefinitely extended arrays of atoms, with regular repeating units that correspond to the formula of the compound: NaCl i.e. one Na+ to every Cl-, and SiO₂, i.e. one Si atom with 4 covalent bonds to every 2 O atoms with 2 covalent bonds each. (Metals are also a 3rd type of giant structure, but these are generally not chemical compounds.)
But yes, you are right that oxygen and nitrogen are both diatomic molecules: O=O and N≡N. Carbon dioxide is a triatomic molecule: O=C=O.
(Oxygen also forms a triatomic molecule, ozone, but that is very reactive and not good to breathe at all - though it does find some application in disinfecting public swimming baths.)