Over a million developers have joined DZone.

NoSQL and a Life Without Schema

· 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.

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.


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.


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.


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);

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


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.


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.


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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}