linux Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused : code example

Example 1: Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :

sudo service mongodb start

Example 2: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :

sudo rm /var/lib/mongodb/mongod.lock