On macOS 10.13.6 with MongoDB 4.0
I was unable to connect to localhost from the mongo shell
I started MongoDB with:
mongod --config /usr/local/etc/mongod.conf
I found that the 'mongod.conf' had:
bindIp: 127.0.0.1
Change my JavaScript connection from localhost to 127.0.0.1 and it worked fine.
The same was occurring with MongoDB Compass too.