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:
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:
foods: [ “pizza”, “ice cream” ]
It’s even possible to have objects within arrays, as follows:
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.
What is Software Testing?Explain its types in detail.
Thanks for sharing this blog.This article gives lot of information.
Mean Stack Online Training
Mean Stack Training in Hyderabad