Skip to content

Instantly share code, notes, and snippets.

@tvdsluijs
Created October 12, 2014 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvdsluijs/64c3d088a3a10cd435e4 to your computer and use it in GitHub Desktop.
Save tvdsluijs/64c3d088a3a10cd435e4 to your computer and use it in GitHub Desktop.
Get data from MongoDB example
$mongo_host = 'mongodb://10.10.10.10/'; //vul hier jou ip adres in van je MongoDB omgeving
$m = new MongoClient($mongo_host);
$db = $m->blog; //maak connectie met database
$collection = $db->visits; // maak connectie met de tabel / collection
//je kan nu bijvoorbeeld al alle records tellen met
$nr = $collection->count();
echo "<p>aantal records in db : " . $nr . "</p>";
//Maar je wilt natuurlijk filteren op de data en deze terug halen
//bijvoorbeeld de data ophalen tussen twee datums of van gisteren
$today = date('Y-m-d 00:00:00');
$yesterday = date('Y-m-d 00:00:00', (time()-24*SECONDS_PER_HOUR));
$start = strtotime($yesterday);
$end = strtotime($today);
//show between dates
$cursor = $collection->find(array("timestamp" => array('$gte' => $start, '$lte' => $end)));
//en doe een foreach door de gegevens heen
foreach ($cursor as $document) {
echo $document["blog_url"] ."<br/>";
}
$m->close($m);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment