<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MongoDB | Nerdpress.org</title>
	<atom:link href="https://nerdpress.org/category/db/mongodb-db/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Mon, 30 Jan 2012 14:28:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Silex and MongoDB simply</title>
		<link>https://nerdpress.org/2012/01/30/silex-and-mongodb-simply/</link>
					<comments>https://nerdpress.org/2012/01/30/silex-and-mongodb-simply/#comments</comments>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Mon, 30 Jan 2012 14:27:34 +0000</pubDate>
				<category><![CDATA[Doctrine ORM]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silex]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[silex]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=2033</guid>

					<description><![CDATA[<p>Using MongoDB in your Silex Project is quite easy. I will show this with my Superleansilexplate and will integrate it there as an example. Since i dont want to integrate MongoDB in Superleansilexplate it will just become an additional gist. Given you have some smaller amount of data like a counter that needs to be &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2012/01/30/silex-and-mongodb-simply/" class="more-link">Continue reading<span class="screen-reader-text"> "Silex and MongoDB simply"</span></a></p>
The post <a href="https://nerdpress.org/2012/01/30/silex-and-mongodb-simply/">Silex and MongoDB simply</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Using MongoDB in your Silex Project is quite easy.</p>
<p>I will show this with my <a href="https://github.com/ivoba/superleansilexplate">Superleansilexplate</a> and will integrate it there as an example.<br />
Since i dont want to integrate MongoDB in Superleansilexplate it will just become an additional <a href="https://gist.github.com/1704512">gist</a>.</p>
<p>Given you have some smaller amount of data like a counter that needs to be stored or other loose coupled datasets, we simply speak to MongoDB &#8220;directly&#8221; and store the data via <a href="https://github.com/doctrine/mongodb">Doctrine MongoDB Abstraction Layer</a>.<br />
Since i presume the Data / Document Structure isnt that complex we dont use <a href="https://github.com/doctrine/mongodb-odm">Doctrine MongoDB ODM</a> (the Object Document Mapper).<br />
If you want to use it instead, try this <a href="https://github.com/docteurklein/SilexExtensions">Silex Extensions</a>.</p>
<p><span id="more-2033"></span></p>
<p>So we use this <a href="https://github.com/fate/Silex-Extensions">SilexExtensions</a> collection and install it via git:</p>
<pre class="brush: bash; title: ; notranslate">
 cd mysilexproject
 git clone git@github.com:fate/Silex-Extensions.git vendor/silex-extension
</pre>
<p>Then we install the doctrine mongodb libary manually:</p>
<pre class="brush: bash; title: ; notranslate">
 git clone --recursive https://github.com/doctrine/mongodb vendor/mongodb
</pre>
<p>The extension collection has some more poviders but you can just ignore them and just focus on the MongoDbExtension.</p>
<p>&#8211;<em> i actually dont like extensions collections. i would prefer to clone each extensions individually. thou its not much code overhead, but when it comes to the vendor dependencies it can become quite messy IMHO</em> &#8211;</p>
<p>Alright now the code.<br />
Edit src/app.php and register the namespace for the extension:</p>
<pre class="brush: php; title: ; notranslate">
 $app&#x5B;'autoloader']-&gt;registerNamespace('SilexExtension', __DIR__ . '/../vendor/silex-extension/src');
</pre>
<p>Then register the MongoDbExtension;</p>
<pre class="brush: php; title: ; notranslate">
 $app-&gt;register(new SilexExtension\MongoDbExtension(), array(
 'mongodb.class_path' =&gt; __DIR__ . '/../vendor/mongodb/lib',
 'mongodb.connection' =&gt; array(
 'server' =&gt; 'mongodb://mysecretuser:mysecretpassw@localhost',
 'options' =&gt; array(),
 'eventmanager' =&gt; function($eventmanager) {
 }
 )
 ));
</pre>
<p>and finally query your MongoDB collection of choice and display the resultset as json:</p>
<pre class="brush: php; title: ; notranslate">
$app-&gt;get('/data-from-mongodb', function() use($app) {

$coll = $app&#x5B;'mongodb']-&gt;selectDatabase('mydb')-&gt;selectCollection('mycoll');
 $query = array(
 'query' =&gt; 'value'
 );
 $sort = array(
 'count' =&gt; -1,
 );
 $r = $coll-&gt;find($query)-&gt;sort($sort)-&gt;toArray();
 $response = new Response();
 $response-&gt;headers-&gt;set('Content-type', 'application/json');
 $response-&gt;setContent(json_encode($r));
 return $response;
 });
</pre>
<p>Easy</p>The post <a href="https://nerdpress.org/2012/01/30/silex-and-mongodb-simply/">Silex and MongoDB simply</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
					<wfw:commentRss>https://nerdpress.org/2012/01/30/silex-and-mongodb-simply/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Migrate to Mongolab</title>
		<link>https://nerdpress.org/2011/09/26/migrate-to-mongolab/</link>
					<comments>https://nerdpress.org/2011/09/26/migrate-to-mongolab/#comments</comments>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Mon, 26 Sep 2011 05:38:07 +0000</pubDate>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[vServer]]></category>
		<category><![CDATA[mongodb]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=1738</guid>

					<description><![CDATA[<p>Recently i ran into RAM troubles on my vserver for some reasons, i encountered the evil: Cannot allocate memory at ... So first i suspected mongodb to use up loads of memory as top showed. But after some recherche work i learned mongodb only -seems- to use a lot of memory. see here and here &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2011/09/26/migrate-to-mongolab/" class="more-link">Continue reading<span class="screen-reader-text"> "Migrate to Mongolab"</span></a></p>
The post <a href="https://nerdpress.org/2011/09/26/migrate-to-mongolab/">Migrate to Mongolab</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Recently i ran into RAM troubles on my vserver for some reasons, i encountered the evil:</p>
<pre class="brush: bash; title: ; notranslate">
Cannot allocate memory at ...
</pre>
<p>So first i suspected <a href="http://www.mongodb.org/">mongodb</a> to use up loads of memory as top showed.</p>
<p>But after some recherche work i learned mongodb only -seems- to use a lot of memory.<br />
see <a href="http://blog.mongodb.org/post/101911655/mongo-db-memory-usage">here</a> and <a href="http://www.mongodb.org/display/DOCS/Checking+Server+Memory+Usage">here</a> and <a href="http://stackoverflow.com/questions/4918443/is-it-ideal-that-mongodb-is-using-150-mb-memory/4918676#4918676">here</a><br />
The actual usage was around 20mb RAM, so mongodb was innocent.</p>
<p>The true RAM monsters were some apache and php-fpm zombies, but thats another story.</p>
<p>While suspecting mongodb i thought about outsourcing the mongodb and i found a free and sufficient offer in <a href="https://mongolab.com">mongolab</a>.<br />
My interests were on and i gave it a try.<br />
The free version has a limit for up to 240MB storage and since my app is just a small counter it should last for some time.<br />
<span id="more-1738"></span></p>
<p>The registration was easy, creation of the database aswell and the administration panel is also pretty cool and informative.<br />
<img fetchpriority="high" decoding="async" class="alignnone size-medium wp-image-1743" style="border-style: initial; border-color: initial;" title="mongolab" src="https://nerdpress.org/wp-content/uploads/2011/09/mongolab-300x167.png" alt="" width="300" height="167" srcset="https://nerdpress.org/wp-content/uploads/2011/09/mongolab-300x167.png 300w, https://nerdpress.org/wp-content/uploads/2011/09/mongolab.png 816w" sizes="(max-width: 300px) 100vw, 300px" /><br />
Then create a user and migrate:</p>
<p>First get my data from the vserver on the vserver:</p>
<pre class="brush: bash; title: ; notranslate">
 mongodump --host 127.0.0.1
</pre>
<p>Then fetch it to my local machine:</p>
<pre class="brush: bash; title: ; notranslate">
 scp -r me@vserver.de:/var/www/vhosts/verrueckte/dump/mongodumpdata .
</pre>
<p>Ups i didnt had no mongo client on my local machine, go get it:</p>
<pre class="brush: bash; title: ; notranslate">
 sudo apt-get install mongodb-clients
</pre>
<p>Now connect to the mongolab via your console</p>
<pre class="brush: bash; title: ; notranslate">
 mongo db123xyz.mongolab.com:12345/thedb -u &lt;user&gt; -p &lt;pw&gt;
</pre>
<p>Hey works, great!<br />
Alright see you later.</p>
<pre class="brush: bash; title: ; notranslate">
 &gt; exit;
</pre>
<p>Now import the dumps to continue the counter there were i stopped.</p>
<pre class="brush: bash; title: ; notranslate">
 cd mongodumpdata
 mongorestore -h db123xyz.mongolab.com:12345 -d thedb -u -p .
</pre>
<p>Good!</p>
<p>Then teach the php app the new connection:</p>
<pre class="brush: php; title: ; notranslate">
 $this-&gt;host = 'mongodb://user:pw@db123xyz.mongolab.com:12345/thedb';
 new mongo($this-&gt;host);
</pre>
<p>There you go! The counter is storing its stuff to the mongolab database.<br />
And i can stop the mongod on my machine and free some memory.</p>
<pre class="brush: bash; title: ; notranslate">
 ps aux | grep mongodb
 kill &lt;PID&gt;
</pre>The post <a href="https://nerdpress.org/2011/09/26/migrate-to-mongolab/">Migrate to Mongolab</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
					<wfw:commentRss>https://nerdpress.org/2011/09/26/migrate-to-mongolab/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Grouping &#038; Sorting in MongoDB</title>
		<link>https://nerdpress.org/2010/07/24/grouping-sorting-in-mongodb/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Sat, 24 Jul 2010 10:45:21 +0000</pubDate>
				<category><![CDATA[DB]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=1073</guid>

					<description><![CDATA[<p>Will man mit PHP und MongoDB soetwas wie &#8220;SQL Aggregate Functions&#8221; umsetzen muss man sich etwas verbiegen. MongoDb hat zwar eine group() function, die in etwa SQLs GROUP BY entspricht, allerdings kann man dies nicht kombinieren mit SORT oder LIMIT bzw den sort() und limit() Funktionen, da diese keinen Cursor zurückgibt sondern direkt ein Array. &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2010/07/24/grouping-sorting-in-mongodb/" class="more-link">Continue reading<span class="screen-reader-text"> "Grouping &#038; Sorting in MongoDB"</span></a></p>
The post <a href="https://nerdpress.org/2010/07/24/grouping-sorting-in-mongodb/">Grouping & Sorting in MongoDB</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Will man mit PHP und MongoDB soetwas wie &#8220;<strong>SQL Aggregate Functions</strong>&#8221; umsetzen muss man sich etwas verbiegen.</p>
<p>MongoDb hat zwar eine <a href="http://www.php.net/manual/de/mongocollection.group.php">group()</a> function, die in etwa SQLs <em>GROUP BY</em> entspricht, allerdings kann man dies nicht kombinieren mit <em>SORT</em> oder <em>LIMIT</em> bzw den <a href="http://www.php.net/manual/de/mongocursor.sort.php">sort()</a> und <a href="http://www.php.net/manual/de/mongocursor.limit.php">limit()</a> Funktionen, da diese keinen Cursor zurückgibt sondern direkt ein Array.</p>
<p>Will man also sortieren und limitieren muss man sich des Map / Reduce features von MongoDB bedienen.</p>
<p><span id="more-1073"></span></p>
<p>Ein Query wie folgt:</p>
<pre class="brush: sql; title: ; notranslate">
SELECT referer.URL, count(*) AS cnt
FROM `referer`
GROUP BY referer.URL
ORDER BY cnt ASC LIMIT 25
</pre>
<p>&#8230;wird dann über <a href="http://www.php.net/manual/de/mongodb.command.php">Command()</a> so umgesetzt:</p>
<pre class="brush: php; title: ; notranslate">
$map = new MongoCode(&quot;function() { emit(this.url,1); }&quot;);
$reduce = new MongoCode(&quot;function(k, vals) { &quot;.
			&quot;var sum = 0;&quot;.
			&quot;for (var i in vals) {&quot;.
			&quot;sum += vals&#x5B;i];&quot;. 
			&quot;}&quot;.
			&quot;return sum; }&quot;);
$r = $this-&gt;db_handler-&gt;command(array(
		    	&quot;mapreduce&quot; =&gt; &quot;referer&quot;, 
		    	&quot;map&quot; =&gt; $map,
		    	&quot;reduce&quot; =&gt; $reduce));
$r = $this-&gt;db_handler-&gt;selectCollection($r&#x5B;'result'])-&gt;find();
$r-&gt;sort(array('value' =&gt; -1));
$r-&gt;limit(25);
$data = array();
foreach ($r as $ref) {
	$data&#x5B;$ref&#x5B;'_id']] = $ref&#x5B;'value'];
}
return $data;
</pre>
<p>In die Reduce Funktion kann man sämtliche Abfrage-Logik packen, was es zu einem ziemlich mächtigem Konstrukt macht.<br />
Hat man sich einmal dran gewöhnt, lässt sich eine Menge damit anstellen. </p>
<p>Danke auch an diesen <a href="http://kylebanker.com/blog/2009/12/mongodb-map-reduce-basics/">Blog</a>.</p>The post <a href="https://nerdpress.org/2010/07/24/grouping-sorting-in-mongodb/">Grouping & Sorting in MongoDB</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
