MongoDB

What is it

MongoDB (from humongous ) is a scalable, high-performance, open source, schema-free, document-oriented database.
– mongodb.org

Thinking in documents

When building applications and data, it’s about more than the DB.

Ideas behind MongoDB

RDBMS’s are sometimes really awkward to fit to some of today’s problems.

MongoDB’s focus is on performance and flexibility and scaling

It is a document-oriented database

EVERYTHING is a document: { ... }

HOWEVER …

JSON and BSON

Everything is expressed in BSON (“binary” JSON)

MongoDB understands JSON natively

JavaScript Object Notation (JSON) is an open, human and machine-readable standard that facilitates data interchange, and along with XML is the main format for data interchange used on the modern web. JSON supports all the basic data types you’d expect: numbers, strings, and boolean values, as well as arrays and hashes.

Document databases such as MongoDB use JSON documents in order to store records, just as tables and rows store records in a relational database.

A JSON database returns query results that can be easily parsed, with little or no transformation, directly by JavaScript and most popular programming languages – reducing the amount of logic you need to build into your application layer.

MongoDB represents JSON documents in binary-encoded format called BSON behind the scenes. BSON extends the JSON model to provide additional data types, ordered fields, and to be efficient for encoding and decoding within different languages.

For example if we were representing blog articles…

{
  "_id": "No-Sushi-Mondays",
  "title": "Why I never eat sushi on Mondays",
  "date": "2017-02-13T21:27:22.104Z",
  "author": {
    "name": "Tony Stark",
    "title": "CEO",
    "company": "Stark Industries"
  },
  "text": "Do you ever wonder just how fresh fish could be when ...",
  "tags": ["sushi", "Mondays", "nausea", "Vibrio parahaemolyticus"],
  "comments": [
    {
      "name": "FishMonger Phil",
      "comment": "What do you know about ..."
    },
    {
      "name": "Dr.Nick",
      "comment": "What's the big deal, a little ..."
    }
  ]
}

To do the same thing with a relational database would require several joins across different relational tables.

And because Mongo DB Doesn’t have to do these joins, it can be more easily distributed across several databases as shards and applications don’t have to know.

Moving from RDB to MongoDB

SLIDES 26-32

var mydoc = {
             _id: ObjectId("5099803df3f4948bd2f98391"),
             name: { first: "Alan", last: "Turing" },
             birth: new Date('Jun 23, 1912'),
             death: new Date('Jun 07, 1954'),
             contribs: [ "Turing machine", "Turing test", "Turingery" ],
             views : NumberLong(1250327)
          }
db.posts.insertOne(mydoc);
var c = {author: “eliot”, date: new Date(), text: “great post!”}
db.posts.update({_id: post._id}, {$push: {comments: c}})

Naming guidelines

SLIDES 33-57