MongoDB on devweb #

For access to MongoDB on devweb, please see the guidance on Requesting Access.

If you have requested MongoDB access, you'll be given an account with the same username as your university DS account (e.g. xxy12345). You can connect from Linux (Cafe, Linux lab machines or CPU nodes - see Remote Compute) by running mongo --host

On Windows lab machines, you can use the Putty SSH client to connect to cafe; you can find a shortcut to cafe (using Putty) on the desktop, labelled SSH Cafe.

Querying from the CLI #

The following code querying MongoDB from the CLI (replace $user and $pass as necessary):

mongo --host
use admin
db.auth("$user", "$pass")
use username
show collections

Querying from Python #

The following code querying MongoDB from Python (replace $user and $pass as necessary):

from pymongo import MongoClient
client = MongoClient('mongodb://$user:$')

Querying from PHP #

See for a more complete tutorial on PHP and MongoDB.

The following code demonstrates reading the latest timestamp from a collection then inserting the current timestamp into that collection using PHP (replace $user and $pass as necessary):

$user = "xxy12345";
$pass = "MyMongoDBPassword";
$host = "";
$mongo = new MongoDB\Client("mongodb://$user:$pass@$db_host:27017/");
$db = $mongo->$db_name;
$coll = $db->testcoll;
$count_before = $coll->count();

$coll->insertOne(['ts' => new MongoDB\BSON\UTCDatetime(), 'source' => 'php']);

$count_after = $coll->count();
$max_ts_unix = $coll->findOne([], ['sort' => ['ts' => -1]])['ts'];
$max_ts = date("Y-m-d\TH:i:s", (string)$max_ts_unix/1000);

echo "<li>count was $count_before, now $count_after; new ts $max_ts</li>\n";

Accessing MongoDB from home / off-campus #

In order to access MongoDB off-campus, you need to tunnel your connection via cafe (ssh) as follows (replacing $username with your DS username):

ssh -L $

You'll then by able to connect your local MongoDB client or app to localhost:27017, which will be tunnelled to devweb2023 via cafe.

See Remote compute for more details.