Background Benzodiazepines (BZDs) and Z drugs are widely used for patients with chronic insomnia. The long term use of BZDs in older patients can cause cognitive impairment and potentially increase the risk of dementia. However, evidence for an association with Z drugs is limited. This study aimed to investigate the association between the risk of cognitive impairment and exposure to Z drugs in older patients with chronic insomnia.Methods We recorded older patients with chronic insomnia who visited the outpatient department of neurology, Beijing Friendship Hospital, and assessed the global cognitive function (MoCA) and five cognitive domains (CVLT, TMT-B, BNT-30, CDT and DST). Multiple regression analysis was conducted to determine the independent factors of cognition, and evaluate the effect of Z drugs use (zolpidem and zopiclone) on cognition.Results A total of 88 subjects were identified. BZDs use (P=0.03, B=2.67, 95% CI 0.36-4.97) and BZDs exposure density (P=0.01, B=1.22, 95% CI 0.33-2.11) were independent risk factors of cognitive impairment in older patients with chronic insomnia. Neither Z drugs use (P=0.11) nor Z drugs exposure density (P=0.92) correlated with global cognitive function. Moreover, compared with BZDs users, there were positive associations between Z drugs use and memory (P=0.00), attention (P=0.00) and verbal function (P=0.04). Additionlly, education level (P=0.02, OR=0.74, 95% CI 0.57-0.95), duration of insomnia (P=0.04, OR=1.05, 95% CI 1.00-1.11) and severity of insomnia (P=0.03, OR=1.27, 95% CI 1.02-1.57) were also independent factors of global cognitive function.Conclusion We found no evidence that Z drugs use and Z drugs exposure density were associated with cognitive impairment in older patients with chronic insomnia. However, the use of BZDs and BZDs exposure density were associated with an increased risk of cognitive impairment. Thus, BZDs use should be avoided,and Z drugs should be prescribed with extreme caution in the elderly.