Fangcang shelter hospitals are quarantine facilities offering primary medical treatment for mild and asymptomatic SARS-CoV-2 cases. Little is known about the age-specific prevalence of insomnia among patients in Fangcang shelter hospitals, particularly in older age groups. This cross-sectional study was conducted in the three largest Fangcang shelter hospitals during the lockdown period, from March to May 2022, in Shanghai. The patients' demographic and medical information was recorded. Insomnia was defined according to the prescriptions for zolpidem and estazolam. The overall and age-specific prevalence and the risk factors of insomnia were investigated through regression models. A total of 2,39,448 patients were included in this study (59.09% of the patients were male, the median age was 42, and 73.41% of the patients were asymptomatic), with the prevalence of insomnia being 3.1%. The prevalence of insomnia varied across different age groups (<18 years: 0.23%, 18-64 years: 2.64%, and ≥65 years: 10.36%). SARS-CoV-2 vaccine, regardless of the number of doses, was significantly associated with a decreased risk of insomnia for the group aged ≥65 years. Three doses of the vaccine reduced the risk of insomnia for patients aged 18-64 years. An extra day in the hospital significantly increased the risk of insomnia by approximately 10% for all age groups. Mild symptoms were significantly associated with a higher risk of insomnia among patients aged <65 years old, while being male and residing in the surrounding area were negatively associated with insomnia for all adults. This study observed that older patients were a high-risk population for developing insomnia in Fangcang shelter hospitals. SARS-CoV-2 vaccination might decrease the risk of insomnia in adults, especially the older adult, which indicates the benefits of vaccination for reducing insomnia among infected patients.