How to connect to remote mongodb with php?

17,585

Solution 1

You can use mongoOd without the REST API But remember, it's a replica Set cluster so You need to configure your PHP for a ReplicaSet configuration...

I use mongoOd within ruby & mongoid (not the REST API)

Here a php example

<?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 :)

A mongoOd Team member

Solution 2

The constructor for the mongo object takes as its arguments connection parameters.

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

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

Solution 3

You'll have to ask them what the connection URI is, and then use:

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

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

At any rate, you should ask them for help. There's a button on the left that reads "aide," if you click on it you'll get a form where you can fill in your email and your question.

Reference: Mongo - Connecting

Share:
17,585

Related videos on Youtube

Hassan
Author by

Hassan

Updated on June 04, 2022

Comments

  • Hassan
    Hassan almost 2 years

    Here is the php code that I'm working with in 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?

  • Hassan
    Hassan over 12 years
    yep, thats what i tryed but they want rest indeed; i will check with them, thanks