The opposite is true for drinking sugary beverages and whole milk, as well as for continually eating snacks and a lot of carbohydrates, researchers said.
Scientists from the University of Groningen in the Netherlands analysed the microbes inside the guts of more than 1,100 people.
They identified 126 factors that were correlated with changes in the makeup of an individual's microbial community - including 60 associated with diet, 12 related to diseases, 19 linked with drugs and four tied to smoking.
Researchers studied stool samples of 1,135 Dutch participants. They collected their own stool samples at home and then immediately put them in the freezer.
Also Read
After a few days, samples were transported to labs at the university, where they remained frozen until they were processed by researchers, 'Los Angeles Times' reported.
This ensured that none of the bacteria had a chance to grow or change from the time the sample was collected and guaranteed that all samples were treated the same way, researchers said.
Sugary sodas and savoury snacks were associated with lower levels of diversity. Similarly, so was having irritable bowel syndrome and smoking during pregnancy, researchers said.
Women and older people tend to have more microbial diversity than men and younger people, they said.
The findings were published in the journal Science.