MongoDB Sorting and Indexes

0

Posted by Joe | Posted in MongoDB | Posted on 20-01-2012

I’ve been looking at some of my MongoDB queries and trying to optimise them. I had one query that did a few ‘in’ statement and then sorted on a field called CreatedTime in descending order to give me the latest documents first.

My index looked something like:

{ “Field1″ : 1, “Field2″ : 1, “Field3″ : 1, “CreatedTime” -1 }

With this index I got the following information from the Explain method:

“nscanned” : 8120, “nscannedObjects” : 4364, “n” : 121, “scanAndOrder” : true, “millis” : 62

I couldn’t really work out why it was scanning my entire index, and also half the documents in the collection. Playing around with the query I found that it was the sorting that was causing it. After removing my sort I get the following result:

“nscanned” : 128, “nscannedObjects” : 126, “n” : 121, “millis” : 2

Much better, although unfortunately in the incorrect order. If I was using a capped collection I could use reverse natural order, but I’m not. I thought that as I had CreatedTime indexed descending, doing a sort on that field descending would be fine, but just for fun I thought I’d put the field as the first field specified in my index to see if that would alter how MongoDB stores and uses the index:

{ “CreatedTime” -1, “Field1″ : 1, “Field2″ : 1, “Field3″ : 1 }

Now running the query gives me the following:

“nscanned” : 256, “nscannedObjects” : 121, “n” : 121, “millis” : 2

That’s more like it, and you see the scanAndOrder attribute is no longer there. I’m limiting the query to 121 results, and the number of documents hit is 121, which is good as that means the query itself only used the index, and the exact documents were only accessed to get the fields I’m returning.

The query is scanning 256 index entries, presumably that how many it had to look at to match 121 documents based on my other query criteria.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

A MongoDB Tutorial using C# and ASP.NET MVC

11

Posted by Joe | Posted in ASP.NET, C#, MongoDB, MVC | Posted on 02-10-2011

In this post I’m going to create a simple ASP.NET MVC website for a simple blog that uses MongoDB and the offical 10gen C# driver.

MongoDB is no NOSQL database that stores information as Binary JSON (BSON) in documents. I have been working with it now for around 6 months on an enterprise application and so far am loving it. Our application is currently in alpha phase but should be public early next year! If you are used to working with an RDBMS, it takes a little bit of getting used to as generally you work with a denormalized schema. This means thinking about things quite differently to how you would previously; you’re going to have repeating data which is a no-no in a relational database, but it’s going to give you awesome performance, sure you may need an offline process that runs nightly and goes and cleans up your data, but for the real time performance gains it’s worth it.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

ASP.NET MVC 2 client side validation for dynamic fields added with ajax

0

Posted by Joe | Posted in Ajax, C#, MVC | Posted on 24-07-2011

It’s quite common to add fields to a form dynamically using ajax, for example you may have a list of phone numbers for a user, and they could have many different phone numbers.

Using MVC you can easily add fields to a form by making an ajax call to an action that returns the rendered result of a partial view, but if you’ve tried doing this when using client side validation you’ll find the client side validation doesn’t work for those fields dynamically added to the form via the ajax call.

In this article I’ll explain a simple way to enable client side validation on those dynamically added fields.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Extract content from HTML and split words into an array using C#

0

Posted by Joe | Posted in C# | Posted on 10-07-2011

The other day I was messing around with Full Text Search with MongoDB (which I’ll probably write about in the future), and wanted to take some HTML content and remove all the HTML tags, extract the actual content, then split up the words into an array.

I wrote the following extension method that seems to do the trick. I’ve not tested it too much so there are likely some scenarios where it won’t work, but for those I’ve tried it’s worked fine.

using System.Text.RegularExpressions;
using System.Web;

public static string[] Tokenize(this string value)
{
    //Remove Html tags
    value = Regex.Replace(value, @"<.*?>", string.Empty);

    //Decode Html characters
    value = HttpUtility.HtmlDecode(value);

    //Remove everything but letters, numbers and whitespace characters
    value = Regex.Replace(value, @"[^\w\s]", string.Empty);

    //Remove multiple whitespace characters
    value = Regex.Replace(value, @"\s+", " ");

    //Trim, set to lower case and split to array
    return value.Trim().ToLower().Split(' ');
}

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Model Binding MongoDB ObjectId with ASP.NET MVC

4

Posted by Joe | Posted in ASP.NET, C#, MongoDB, MVC | Posted on 12-06-2011

If you’re using the MongoDB C# driver with ASP.NET MVC and have a property of type ObjectId in your model you will get the following error when trying to bind back to the model.

The parameter conversion from type ‘System.String’ to type ‘MongoDB.Bson.ObjectId’ failed because no type converter can convert between these types.

This can easily be resolved by creating a custom Model Binder which I’ll explain how to do in this article.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Creating strongly typed wrappers around untyped dictionaries in ASP.NET using the Castle DictionaryAdapter

0

Posted by Joe | Posted in ASP.NET, C#, IoC, MVC | Posted on 09-06-2011

Often when writing web applications you find yourself writing strongly typed wrapper classes around untyped dictionaries, such as Session, QueryString or even Web.Config Application Settings.

This is quite a tedious task, but using the Castle DictionaryAdapter this is all wrapped up nicely, and all you need to do is create an interface.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

MongoDB windows service won’t start

0

Posted by Joe | Posted in MongoDB | Posted on 07-06-2011

A couple of times now my MongoDB windows service won’t start after rebooting my machine. If you look at it in the services list it flicks between Starting and Started states but never really startsm and there doesn’t seem to be a way to get it to stop.

To fix this delete the mongod.lock file in your MongoDB data directory. It will immediately create a new one, but then the service will start.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Editing and binding nested lists with ASP.NET MVC 2

12

Posted by Joe | Posted in Ajax, ASP.NET, C#, JavaScript, jQuery, MVC | Posted on 06-06-2011

Dynamically editing lists of data and binding back to the model with MVC is a little complicated as the id’s of the form elements need to all tie up for binding to succeed. Recently I had a model, which contained a list of an object, which in turn contained another nested list. Getting this to easily bind back to the model when adding to the lists dynamically was a bit of a headache so I’ll explain how I did it.

This article is inspired by this article by Steve Sanderson, but I also explain how to adapt it to bind nested lists.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

How to register and get all instances that implement an interface using Autofac

2

Posted by Joe | Posted in C# | Posted on 30-05-2011

I’ve just started using Autofac, where I’ve previously used Ninject and StructureMap.

UPDATE (09/06/2001): Although this post is about registering all instances that implement a specific interface, my reason for doing so was to create a bootstrapper task. I’ve recently found AutoFac has an IStartable interface which basically does the same thing.

I have an interface called IBootStrapperTask that looks like this:

public interface IBootStrapperTask
{
    void Execute();
}

I then have several tasks that implement this interface, which I want to get and run when my application starts. First I need to register all types that use this interface, which I don’t want to do by automatically registering all types. I can do this like so:

var builder = new ContainerBuilder();

builder.RegisterAssemblyTypes(Assembly.Load("MyAssesmbly")).Where(t => typeof(IBootStrapperTask).IsAssignableFrom(t)).InstancePerLifetimeScope().AsImplementedInterfaces();

From what I can tell you can only do this on a per assembly basis. Now I have all my types registered I can get and execute them all:

var container = builder.Build();

var tasks = container.Resolve<IEnumerable<IBootStrapperTask>>();
foreach (var task in tasks)
{
    task.Execute();
}

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

ASP.NET MVC simple server-side ajax paging using jQuery

18

Posted by Joe | Posted in Ajax, ASP.NET, C#, jQuery, MVC | Posted on 30-05-2011

It’s always important to page your data on the server side so that you are only hitting the database to get the page you currently need, rather than getting all the results and paging on the client. In this post I’ll explain how to perform simple ajax paging on the server side using ASP.NET MVC 3.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit