```python
streamlit_app.py
import streamlit as st
import pymongo
Initialize connection.
client = pymongo.MongoClient(**st.secrets["mongo"])
Pull data from the collection.
Uses st.cache to only rerun when the query changes or after 10 min.
@st.cache(ttl=600)
def get_data():
    db = client.mydb
    items = db.mycollection.find()
    items = list(items)  # make hashable for st.cache
    return items
items = get_data()
Print results.
for item in items:
    st.write(f"{item['name']} has a :{item['pet']}:")
```
See st.cache above? Without it, Streamlit would run the query every time the app reruns (e.g. on a widget interaction). With st.cache, it only runs when the query changes or after 10 minutes (that's what ttl is for). Watch out: If your database updates more frequently, you should adapt ttl or remove caching so viewers always see the latest data. Read more about caching here.
