Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I've probably spent more time than I should on this. I want to deploy PHP & mongodb applications with the help of Heroku.

Heroku mLab provides me the environment variable MONGODB_URI for accessing in PHP through getenv('MONGODB_URI') and this environment contains standard MongoDB URI:

mongodb://<dbuser>:<dbpassword>@dsxxxxxx.mlab.com:31899/heroku_lxxxxxx

which already contains an existing database name, but I have connected the string without it, as mentioned below:

$client = new MongoDB\Client("mongodb://localhost:27017");

$collection = $client->demo->beers;

So can I use the default Heroku's MONGODB_URI string or I have to parse_uri it and then pass that to PHP?

1 Answer

0 votes
by (107k points)
edited by

In MongoDB, the string “mongo://localhost:27017” basically means that the drive is connected to the localhost:27017 without any authentication whereas the Heroku manages your data in different servers and in that server it requires authentication. As you have mentioned your MONGODB_URI so your code will be:

$mongoHost = getenv('MONGODB_URI');

$client = new MongoDB\Client($mongoHost);

Learn about full stack web development by signing up for this professional Full Stack Training offered by Intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Oct 18, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer
asked Feb 19, 2020 in Web Technology by ashely (50.2k points)
0 votes
3 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...