Joe Karlsson
progress. tech. cats. coffee. art. food. photog. mpls.
Intro to Document Stores
NoSQL refers to the storage and retrieval of persistent data that are not stored in related tables.
DB Management Systems that fall under this term are
Key-Value, Graph, or Document Stores
a.k.a. Document-oriented Database
Every database management system that utilizes the idea of storing data as "documents", store these documents in the following formats
{
"_id" : ObjectId("55affc8487df4e4899702589"),
"username" : "qui-gon-jinn-and-juice",
"firstName" : "Anakin",
"lastName" : "SkyTroller",
"password" : "padawonton"
}
RDBMS stores records as rows in related tables
Document-Oriented DBs store Documents in unrelated Collections
Tables define columns, their data type, have keys and constraints.
Collections are schema-less at the db layer.
Tables can be indexed
Collections can be indexed
Indexes increase the performance of queries.
db.jedis.createIndex({ name : "text"});
db.weapons.createIndex({ weight : 1 });
CREATE INDEX jedi_name_idx ON jedis (name);
Tables have a predefined schema that all contained rows must abide by. Each column is defined with strict data types
Documents store unstructured data in fields that do not have to conform to a predefined schema.
Rows in tables must follow the predefined schema, if the table schema changes, every row in the table adopts these changes.
Individual Documents can be updated at any time to remove existing or add new fields.
db.jedis.insert({ username: "mace808", firstName:"Mace"});
db.jedis.insert({ name: { first: "Obi Wan", last: "Kenobi"}, force : 1200 });
CREATE TABLE jedis (
id serial,
username varchar(50) NOT NULL,
firstName varchar(50) NOT NULL,
lastName varchar(50) NOT NULL,
password varchar(32) NOT NULL,
weapon_id integer,
PRIMARY KEY id
);
Tables can set foreign key constraints to track relationships
Documents do not have foreign keys to manage relations
Table schemas can be normalized to reduce redundancy and disk usage.
Documents can be denormalized to increase data duplication and disk usage, and can increase data read/retrieval performance.
CREATE TABLE orders (
id integer PRIMARY KEY,
customer_id integer REFERENCES products (id),
price integer
);
Tables can join other tables using foreign keys, to include related data in the resulting rows.
Documents can embed other documents!
Table rows cannot embed other rows.
{
"_id" : ObjectId("55affc8487df4e4899702589"),
"username" : "qui-gon-jinn-and-juice",
"firstName" : "Anakin",
"lastName" : "SkyTroller",
"password" : "padawonton"
"weapon" : {
"_id" : ObjectId("21zffc8487df4e4899702589"),
"name" : "Light Saber",
"weight" : 59.634
},
"friends" : [
{ "_id" : ObjectId("44bffc8487df4e4899702589"), ... },
{ "_id" : ObjectId("33cffc8487df4e4899702589"), ... },
]
}
use the declarative Structured Query Language for defining and manipulating the data.
use an unstructured query language that varies from system to system, and can be imperative.
var usersCur = db.users.find();
var firstUser = usersCur.next();
var firstUserName = firstUser.username;
SELECT username FROM users LIMIT 1;
store data in tables with a fixed schema with strict types
store data in collections where each entry (document, value, node, or row) can have dynamic schemas, they can differ from entries belonging to the same collection
most rdbms are ACID compliant
Atomicity, Consistency, Isolation, Durability,
and support transactions.
most do are not ACID compliant, in favor of performance and scalability
typically use vertical scaling strategies.
the more data you have to store,
the bigger your server gets.
are designed to scale horizontally,
across multiple servers.
By Joe Karlsson
Intro