Created
October 12, 2014 14:57
-
-
Save tvdsluijs/64c3d088a3a10cd435e4 to your computer and use it in GitHub Desktop.
Get data from MongoDB example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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