Over a million developers have joined DZone.

Awesome Indexing with RavenDB

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

I am currently teaching a course in RavenDB, and as usual during a course, we keep doing a lot of work that pushes what we do with RavenDB. Usually because we try to come up with new scenarios on the fly and adapting to the questions from the students.

In this case, we were going over the map/reduce stack and we kept coming more and more complex example and how to handle them, and then we got to this scenario.

Given the following class structure:

   1: public class Animal
 {
     public string Name { get; set; }
     public string Species { get; set; }
     public string Breed { get; set; }
 }

Give me the count of all the species and all the breeds.  That is pretty easy to do, right?  In SQL, you would write it like this:

 SELECT Species, Breed, Count(*) FROM Animals
 GROUP BY Species, Breed

And that is nice, but it still means that you have to do some work on the client side to merge things up to get the final result, since we want something like this:

  • Dogs: 6
    • German Shepherd: 3
    • Labrador: 1
    • Mixed: 2
  • Cats: 3
    • Street: 2
    • Long Haired: 1

In RavenDB, we can express the whole thing in a simple succinct index:

 public class Animals_Stats : AbstractIndexCreationTask<Animal, Animals_Stats.ReduceResult>
 {
     public class ReduceResult
     {
         public string Species { get; set; }
         public int Count { get; set; }
         public BreedStats[] Breeds { get; set; }
  
         public class BreedStats
         {
             public string Breed { get; set; }
             public int Count { get; set; }
         }
     }

     public Animals_Stats()
     {
         Map = animals =>
                 from animal in animals
                 select new
                     {
                         animal.Species,
                         Count = 1,
                         Breeds = new [] {new {animal.Breed, Count = 1}}
                     };
         Reduce = animals =>
                     from r in animals
                     group r by r.Species
                     into g
                     select new
                         {
                             Species = g.Key,
                             Count = g.Sum(x => x.Count),
                             Breeds = from breed in g.SelectMany(x => x.Breeds)
                                     group breed by breed.Breed
                                     into gb
                                     select new {Breed = gb.Key, Count = gb.Sum(x => x.Count)}
                        };
  
     }
 }

And the result of this beauty?

image

And that is quite pretty, even if I say so myself.




Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of Ayende Rahien, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}