Due to the widespread of pathogens and infectious diseases, rapid detection and antibacterial agents are needed. Antibacterial nanomaterials have attracted attention due to their advantageous properties and unique mechanisms. Besides, the green route approach to synthesizing nanoparticles has attracted interest due to being environmentally friendly, biocompatible, and non-toxic. In this study, carbon dots (Cdots), a promising antibacterial material, have been combined with the Fe3O4 nanoparticles as the carrier and produce Fe3O4/Cdots nanocomposite via green synthesis for bacteria detection and antibacterial agents. Green synthesis was achieved with Moringa oleifera leaves extract as reducing agent on Fe3O4 and watermelon peel as carbon source on Cdots. X-ray diffraction shows a cubic inverse spinel structure of magnetite. The phase composition of the nanocomposites consists of magnetite and carbon. The particle size of nanocomposites is 13.4 nm with semi-spherical morphology and high surface area. The bonding analysis and element composition are consistent with phase composition estimation that confirms the existence of Fe3O4 and Cdots on the nanocomposites and lacks other impurities. The vibrating sample magnetometer shows the superparamagnetic behavior of the samples. Nanocomposites were used to detect Escherichia coli (E. coli) bacteria. The quenching of photoluminescence of the nanocomposite shows an increase in the amount of E. coli bacteria. Furthermore, nanocomposites produce high inhibition zones against E. coli bacteria within 48 h, offering excellent antibacterial properties. Hence, nanocomposites provide a promising agent for bacteria detection and antibacterial treatment.