This is an example for downgrading from MongoDB version 3.6 to version 3.4 in Ubuntu 18.04 LTS, but it can be adapted for any combination of versions.
Remove old versions. Since this was done for a compatibility issue with UniFI Controller, the first step is optional
sudo apt-get remove unifi
sudo apt-get remove mongo* sudo apt-get autoremove
Install MongoDB v3.4 Community Edition using the guide below. They also provide guides specific to other versions that you may need.
https://docs.mongodb.com/v3.4/tutorial/install-mongodb-on-ubuntu/
If you are doing this for UniFi Controller (optional), download latest controller package for Ubuntu
https://www.ubnt.com/download/unifi#
Install it (optional)
sudo dpkg -i unifi_sysvinit_all.deb
Fix any broken packages
apt --fix-broken install