Email Subscription Form

Saturday, July 6, 2019

Testing With Non-Relational Databases

Last week, I took a look at ways to query relational databases for testing.  This week I'm going to look at non-relational databases, describe how they are different from relational databases, and discuss how to query them in your testing.  Non-relational databases, such as MongoDB and DynamoDB, are sometimes called "NoSQL" databases, and are becoming increasingly popular in software applications.



The main difference between relational and non-relational databases is that relational databases use tables to store their data, where non-relational tables use documents.  The documents are often in JSON format.  Let's take a look at what the records in the Contacts table from last week's post would look like if they were in a non-relational database:


{
              contactId: "10000",
              firstName: "Prunella",
              lastName: "Prunewhip",
              email: "pprunewhip@fake.com",
              phone: "8005551000",
              city: "Phoenix",
              state: "AZ"
}
{
              contactId: "10001",
              firstName: "Joe",
              lastName: "Schmoe",
              email: "jschmoe@alsofake.com",
              state: "RI",
}

Note that Joe does not have a value for phone or city entered, so they are not included in his document.  This is different from relational databases, which are required to include a value for every field. Instead of having a NULL value for phone and city as Joe's record did in the SQL table, those fields are simply not listed.

Another key difference between relational and non-relational databases is that it's possible to add a new field into a table without adding it in for every document.  Let's imagine that we are adding a new record to the table, and we want that record to include a spouse's name.  When that record is added, it will look like this:

{
              contactId: "10002",
              firstName: "Amy",
              lastName: "Smith",
              email: "amysmith@faketoo.com",
              phone: "8885551001",
              city: "Boise",
              state: "ID",
              spouse: "John"
}

The original documents, 10000 and 10001, don't need to have this spouse value.  In a relational database if a new field is added, the entire schema of the table needs to be altered, and Prunella and Joe will need to have spouse values or NULL entered in for those fields.

With a non-relational database, it's not possible to do joins on table data as you saw in last week's post.  Each record should be treated as its own separate document, and you can do queries to retrieve the documents you want.  What that query language looks like depends on the type of the database used.  The examples below are using MongoDB's query language, which is JavaScript-based, and are querying on the documents listed above:

db.contacts.find() - this will return all the contacts in the table
db.contacts.find( { contactId: "10001" } ) - this will return the document for Joe Schmoe

To make the responses easier to read, you can append the command .pretty(), which will organize the data returned in JSON format rather than a single line of values. 

You can also run a query to return a single field for each document:

db.contacts.find({}, {firstName:1, _id:0}) - this will return just the first name for each contact

Because the documents in a non-relational database have a JSON-like structure, it's possible to have documents with arrays.  For example, our Contacts table could have a document that lists the contact's favorite foods:

{
              contactId: "10000",
              firstName: "Prunella",
              lastName: "Prunewhip",
              email: "pprunewhip@fake.com",
              phone: "8005551000",
              city: "Phoenix",
              state: "AZ",
              foods: [ "pizza", "ice cream" ]
}

It's even possible to have objects within arrays, as follows:

{
              contactId: "10001",
              firstName: "Joe",
              lastName: "Schmoe",
              email: "jschmoe@alsofake.com",
              state: "RI",
              pets: [ { type: "dog", name: "fido" }, { type: "cat", name: "fluffy" } ]
}

You can see how this type of data storage might be advantageous for your application's data.  Nesting data in this fashion makes it easier to read at a glance than it would be in a relational database, where the pets might be in their own separate table.

To run a query that will return all the contacts that have cats, you would simply request:

db.contacts.find( {"pets.type":"cat"} )

To run a query that will return all the contacts that have cats named Fluffy, you would request:

db.contacts.find( {$and: [{"pets.type":"cat"},{"pets.name":"fluffy"}]} )

These are just a few simple examples of how to query data with a non-relational database, and they should be enough to get you started in your testing.  To learn more, be sure to read the documentation for the type of database you are using.  As non-relational databases become increasingly popular, this knowledge will be extremely useful.  


No comments:

Post a Comment

The Easiest MongoDB Tutorial on the Web

MongoDB is one of the most popular non-relational databases in use today.  Its versatility, speed, and scalability make it popular with appl...