While connected to a wifi network, mongodb://localhost/db_name
worked as expected.
When I wasn't connected to any wifi network, this couldn't work. Instead I used,
mongodb://127.0.0.1/db_name
and it worked.
Probably a problem to do with ip configurations.