How to connect to remote mongodb with php?

Here is the PHP code I'm working on on my local machine:

$m = new Mongo(); $db=$m->selectDB("def"); //then all in my code i use $db to select insert ... (as defined in php doc) 

Now I want to connect my application to a remote server (hosted by mongood.com)

How can i do this?

+4
source share
3 answers

You can use mongoOd without the REST API. But remember that this is a Replica Set, so you need to configure PHP to configure ReplicaSet ...

I use mongoOd inside ruby ​​and mongoid (not REST API)

Here is an example php

 <?php // connecting to mongood.com cluster $m = new Mongo("mongodb://94.23.54.103:27017,188.165.219.99:27017,94.23.220.151:27017", array("replicaSet" => "cluster")); var_dump($m); $db = $m->selectDB('my_database'); $db->authenticate("my_login", "my_password"); $collection = new MongoCollection($db, 'my_collection'); $cursor = $collection->find(); foreach ($cursor as $doc) { var_dump($doc); } ?> 

Enjoy :)

MongoOd team member

+8
source

You need to ask them what the connection URI is, and then use:

 $m = new Mongo("mongodb://username: password@hostname "); 

However, I am not sure if this option is available to you. Their website says that you can access data through the REST API.

In any case, you should ask them for help. There is a button on the left that reads "assistant", if you click on it, you will get a form in which you can fill in your email address and your question.

Link: Mongo - Connection

+2
source

The mongo object constructor accepts arguments as connection parameters.

http://www.php.net/manual/en/mongo.construct.php

$m = new Mongo('mongodb://[username:password]@host:port')

+2
source

Source: https://habr.com/ru/post/1381610/


All Articles