<?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>Schotime.net &#187; NoRM</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/norm/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Sun, 20 Nov 2011 01:44:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NoRM, MongoDb and Complex Queries</title>
		<link>http://schotime.net/blog/index.php/2010/05/14/norm-mongodb-and-complex-queries/</link>
		<comments>http://schotime.net/blog/index.php/2010/05/14/norm-mongodb-and-complex-queries/#comments</comments>
		<pubDate>Fri, 14 May 2010 08:47:18 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[NoRM]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2010/05/14/norm-mongodb-and-complex-queries/</guid>
		<description><![CDATA[In my last post I told you about how you can use Regular Expressions to do complex queries in mongodb. In this post I will show you some more features of the Linq Provider. Updated: 16th May 2010 after some feedback Take these two classes for example: public class User { public string Id { [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/?referer=');">post</a> I told you about how you can use Regular Expressions to do complex queries in mongodb. In this post I will show you some more features of the Linq Provider.</p>
<p><strong>Updated: 16th May 2010 after some feedback</strong></p>
<p>Take these two classes for example:</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400"> 
<pre class="code"><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">User
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Id { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Name { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #6897bb">IList</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">Role</span><span style="background: black; color: white">&gt; Roles { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
    }

    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">Role
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Name { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
        </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">AccessLevel { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>and given the following data:</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">standardRole = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">Role </span><span style="background: black; color: white">{Name = </span><span style="background: black; color: #a5c25c">&quot;Standard&quot;</span><span style="background: black; color: white">, AccessLevel = </span><span style="background: black; color: #6897bb">2</span><span style="background: black; color: white">};
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">adminRole = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">Role </span><span style="background: black; color: white">{ Name = </span><span style="background: black; color: #a5c25c">&quot;Standard&quot;</span><span style="background: black; color: white">, AccessLevel = </span><span style="background: black; color: #6897bb">5 </span><span style="background: black; color: white">};
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">user1 = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">User</span><span style="background: black; color: white">()
            {
                Id = </span><span style="background: black; color: #a5c25c">&quot;jbloggs&quot;</span><span style="background: black; color: white">,
                Name = </span><span style="background: black; color: #a5c25c">&quot;Joe Bloggs&quot;</span><span style="background: black; color: white">,
                Roles = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">Role</span><span style="background: black; color: white">&gt;
                            {
                                standardRole,
                                adminRole
                            }
            };

            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">user2 = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">User</span><span style="background: black; color: white">()
            {
                Id = </span><span style="background: black; color: #a5c25c">&quot;tsmith&quot;</span><span style="background: black; color: white">,
                Name = </span><span style="background: black; color: #a5c25c">&quot;Tony Smith&quot;</span><span style="background: black; color: white">,
                Roles = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">Role</span><span style="background: black; color: white">&gt;
                            {
                                standardRole,
                            }
            };</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Now say I want to find all users with access level greater than or equal to 5. In Linq-2-sql or plain sql your might write this query like the following. </p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
          </p>
<pre class="code"><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">query = list.Where(x =&gt; x.Roles.Where(y =&gt; y.AccessLevel &gt;= </span><span style="background: black; color: #6897bb">5</span><span style="background: black; color: white">).Count() &gt; </span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This query will not work in the NoRM linq provider, however there is an alternative.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre><span style="background: black; color: #cc7832">1. var </span><span style="background: black; color: white">queryforMongo = list.Where(x =&gt; x.Roles[0].AccessLevel &gt;= </span><span style="background: black; color: #6897bb">5</span><span style="background: black; color: white">).ToList();or</span></pre>
<pre class="code"><span style="background: black; color: #cc7832">2. var </span><span style="background: black; color: white">queryforMongo = list.Where(x =&gt; x.Roles.Any(y=&gt;y.AccessLevel &gt;= </span><span style="background: black; color: #6897bb">5)</span><span style="background: black; color: white">).ToList();</span></pre>
<pre><span style="background: black; color: white"></span><span style="background: black; color: white"></span></pre>
</td>
</tr>
</tbody>
</table>
<p>Query 1 here will only return Users where the first role has an access level greater than or equal to 5.<br />
  <br />Query 2 will return Users where 1 of the Roles has an access level greater than or equal to 5.</p>
<p><strong>One caveat though.</strong> If you try and do a complex query ( one involving an “or” condition or a “and” using the same property ) then you will get an exception. This is because complex queries get converted to a javascript function that cannot be used in the same ways. </p>
<p>Hope this comes in handy for folks out there. </p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2010/05/14/norm-mongodb-and-complex-queries/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>NOSQL, NoRM (mongoDB) and Regular Expressions</title>
		<link>http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/</link>
		<comments>http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 07:57:29 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[NoRM]]></category>
		<category><![CDATA[Regex]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/</guid>
		<description><![CDATA[Over the past few weeks I have got quite involved in the development of the Open Source NoRM project started by Andrew Theken which is a MongoDB driver for C#. In particular I have been refactoring and adding new functionality to the LINQ provider. It currently supports a lot of functionality including deep queries, regex, [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past few weeks I have got quite involved in the development of the Open Source <a href="http://github.com/atheken/NoRM" onclick="pageTracker._trackPageview('/outgoing/github.com/atheken/NoRM?referer=');">NoRM</a> project started by <a href="http://andrewtheken.com/" onclick="pageTracker._trackPageview('/outgoing/andrewtheken.com/?referer=');">Andrew Theken</a> which is a <a href="http://www.mongodb.org/" onclick="pageTracker._trackPageview('/outgoing/www.mongodb.org/?referer=');">MongoDB</a> driver for C#. In particular I have been refactoring and adding new functionality to the LINQ provider.</p>
<p>It currently supports a lot of functionality including deep queries, regex, datetime which is really exciting. In this post though I am going to concentrate on regular expressions.</p>
<p>This is the newest part of the Linq provider however it is probably the most powerful, especially for complex queries. The reason for this is that MongoDB will use the indexes created when a regular expression is used (where the query is not a complex query). A complex query is one that filters on the same property twice, uses a string function (replace/substring/toLower etc) or does some other fancy stuff. For example using the toUpper() method.</p>
<table style="background: black;" border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="background: black; color: #cc7832;">var </span><span style="background: black; color: white;">products = session.Products.Where(x =&gt; x.Name.ToUpper() == </span><span style="background: black; color: #a5c25c;">"TEST3"</span><span style="background: black; color: white;">).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Anyways….i digress. So, here is an example of using a Regex in a Linq Query. Pretty simple.</p>
<table style="background: black;" border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="background: black; color: #cc7832;">var </span><span style="background: black; color: white;">products = session.Products.Where(p =&gt; </span><span style="background: black; color: #ffc66d;">Regex</span><span style="background: black; color: white;">.IsMatch(p.Name, </span><span style="background: black; color: #a5c25c;">"^te"</span><span style="background: black; color: white;">)).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Using the static Regex.IsMatch is the only way to invoke a regex call using the Linq Provider. This will however run blazingly fast. I tested this query on 1,000,000 Products and it only took 1.5sec, which was approximately 10x faster than when a complex query is invoked. There are however 3 string functions that have been optimized using this regex functionality. They are StartsWith(), EndsWith() and Contains() which is why the following query only takes 2secs to return over 48,000 rows, however when using Skip() and Take() you can get 50 results back in just milliseconds.</p>
<table style="background: black;" border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="background: black; color: #cc7832;">var </span><span style="background: black; color: white;">products = session.Products.Where(x =&gt; x.Name.StartsWith(</span><span style="background: black; color: #a5c25c;">"X"</span><span style="background: black; color: white;">)).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>The Linq provider also supports 3 of the RegexOptions. They are RegexOptions.IgnoreCase (but please note this will not use the index so will be slower), RegexOptions.Multiline and RegexOptions.None. Regex’s can also be used in conjunction with other filters. eg.</p>
<table style="background: black;" border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top"><span style="background: black; color: #cc7832;"></p>
<pre class="code">var <span style="background: black; color: white;">products = session.Products.Where(p =&gt; </span><span style="background: black; color: #ffc66d;">Regex</span><span style="background: black; color: white;">.IsMatch(p.Name, </span><span style="background: black; color: #a5c25c;">"^te"</span><span style="background: black; color: white;">) &amp;&amp; p.Price == </span><span style="background: black; color: #6897bb;">10</span><span style="background: black; color: white;">).ToList();</span></pre>
<p></span></td>
</tr>
</tbody>
</table>
<p>This query is not considered a complex query because two different properties are used and an “and”(&amp;&amp;) operator is used.</p>
<p><strong><em>Please note:</em></strong> Any time a “or” (||) operator is used, it will be considered a complex query.</p>
<p><strong>Summary:</strong></p>
<p><strong> </strong></p>
<p>If you have a filter than can be written as a regex, chances are it will be as fast or faster than without using a regex.</p>
<p>So please go and try out NoRM and enjoy the freedom. I will try and post some more cool stuff in the LINQ provider over the next few weeks. Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

