Share this post on:

Hello everybody! I hope everything is allright on your side and that you are in good spirit and shape! From my side, I had and still have quite a busy period at work, I was also busy playing Assassins Creed Valhalla and nevertheless I went for 2 weeks in Nepal to do the Everest Base Camp trekking.

The passion to learn more is still deep inside me, that’s why today I would like to show you a bit about my current noSQL database, RavenDB and more specifically how to load other documents to be used inside a Javascript RavenDB index. In my work, I like to use the JS indexes because I find them more powerful and the syntax is more familiar plus you can use functions available in Javascript.

Prerequisites

In order to follow the upcoming steps, you’ll need the following:

  • A running instance of RavenDB 5.3
  • A sample database (do not use your production or staging databases here)
  • We’ll use the advanced RavenDB Studio for the demo

Everybody needs friends and so our sample model will do

Let’s design some sample documents that represent people. Each person will have a name, location and an array of people who represent his friends list. We will then create an index of people and his friends which will be displayed in a nice way.

This is how we add a person from RavenDB Studio. It’s important to pay attention to the IDs that you supply to each person, make sure the values in the friends array match the actual values of the people saved in RavenDB. For simplicity, I chose to customize the id to be personX for each person, although it’s up to you if you want to have different values, just make sure to update them in the friends array correctly.

Finally our persons collection should look like this:

[{
    "name": "Johnny Bravo",
    "location": "US",
    "friends": [
        "person2",
        "person4"
    ],
    "@metadata": {
        "@collection": "People"
    }
},
{
    "name": "Jakamoto Sujicucu",
    "location": "JP",
    "friends": [
        "person1"
    ],
    "@metadata": {
        "@collection": "People"
    }
},
{
    "name": "Maria Muierescu",
    "location": "RO",
    "friends": [],
    "@metadata": {
        "@collection": "People"
    }
},
{
    "name": "Chris Borrington",
    "location": "IR",
    "friends": [
        "person1"
    ],
    "@metadata": {
        "@collection": "People"
    }
}]

Allright, now let’s head to the indexing part.

The Javascript index

Now that we have our data in store, let’s head to the index creation. In it’s simplistic form, the index would look like this:

map('People', p => {
    return {
        name: p.name,
        location: p.location,
        friends: p.friends
    };
})

At this point, if we query this newly created index, it will show us the exact same data as if we would query the collection directly. This is normal because the index projects the very same properties of the original documents.

More than that, our friends list shows only the IDs of the people a person is friend to which is not really what we want in this scenario. In order to achieve this, we will need to use the load function to actually load the document by ID and show the names if the people instead. Here’s how the index looks like:

map('People', p => {
    return {
        name: p.name,
        location: p.location,
        friends: p.friends.map(friendId => load(friendId, 'People').name)
    };
})

The difference is that we are mapping the friends ID array to load the actual document with the load function which is available in RavenDB. Once we save this newly updated index we can query it again and we will see that the array will now contain the names of the friends instead of just the IDs.

One odd thing would be that they appear in lowercase, it’s just how an index works by default in RavenDB. Every database system has its own quirks actually 😀 You can overcome this issue if you change the indexing settings for each field to be EXACT, like demonstrated below:

And there you go, you can see the results properly:

My conclusion is that I like Raven

Well, I really like RavenDB with all the features it’s got. I agree that indexes are a bit hard to grasp in the beginning, but after you understand them properly you’ll really love them. There are lots of other things to explore like full text search, time series or spatial data and I will expand those into some articles in the future. I hope you learned something new today and don’t hesitate to contact me or drop a comment below if you need any clarifications.

Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. I also invite you subscribe to my BuyMeACoffee page. Any support is greatly appreciated! Cheers  😉

Avatar afivan

Author: afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored 😂

Leave a Reply