Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

NoSQL and a Life Without Schema

DZone's Guide to

NoSQL and a Life Without Schema

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

Today I want to point out how cool is having no schema when it is time to add properties to your documents.

Suppose that your Player entity changed and you add a new property called Description and you already saved some Players on RavenDb. The question is: what happens when you try to save a new Player now that the class has another property? What happens when you load an old entity, that was saved when that property did not exist?

Since we have no schema we have absolutely no problem, just save another object to Raven and you can find that it got saved without even a warning.

image

Figure 1: New object is saved without any problem even if it is different from the old one

If you double click a Document (Raven call each object saved to the db a Document) you can simply look at its content and the new property called Description got saved without any problem.

image

Figure 2: The object contains the new Description property

Probably you had already recognized JSON format to store the object, but the question is, what happened to the old object saved before you added the Description property ? The answer is: it is still there, clearly without the Description property.

image

Figure 3: The old object does not contain the Description property, but it can coexist with the new one without any problem

Now if you load all object from the database, you can find that for this instance Description property is simply null, because it did not exists in the database, but no error or warning occurred. If you simply load all Player entities and finally issue a SaveChanges, as shown in the following snippet:

 using (var session = store.OpenSession())
 {
     foreach (var player in session.Query<Player>().ToList())
    {
         Console.WriteLine(player.Name + " " + player.RegistrationDate + " " + player.Description);
     }
     session.SaveChanges();
 }

you will end with an automatic update of object and now if you look in the database you can see in figure xxx now the player entity has a Description property equal to null

image

Figure 4: If you ask to SaveChanges, the old player object with id:1 is updated in Raven, now the Description property is present.

The same happens for removed properties, they simply are ignored during the load process, and removed during the save.

This really trivial example shows how simple it is to deal to add or remove a property from documents when your data storage is a NO SQL one and you have no need to define a schema.

 

It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

Topics:

Published at DZone with permission of Ricci Gian Maria. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}