0% found this document useful (0 votes)
7 views

01 - Mongodb - Intro

Uploaded by

emajere
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

01 - Mongodb - Intro

Uploaded by

emajere
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

What is MongoDB?

What is MongoDB?

MongoDB: A document database


MongoDB is an open-source general purpose document database.
MongoDB (from "humongous") is an open-source document database, and the leading NoSQL
database, written in C++.

MongoDB is an open-source document database that provides high performance, high


availability, and automatic scaling.

MongoDB is an open-source, document database designed for ease of development and


scaling.

The software company 10gen began developing MongoDB in 2007 as a component of a


planned platform as a service product.

In 2009, the company shifted to an open source development model, with the company offering
commercial support and other services. In 2013, 10gen changed its name to MongoDB Inc.

4
What is MongoDB?

MongoDB: A document database


{
name: “John Smith”,
pfxs: [“Dr.”,”Mr.”],
address: “10 3rd St.”,
phones: [
{ number: “555-1212”,
type: “home” },
{ number: “444-1212”,
type: “mobile” }
]
}

Fully Featured Document Open


High Performance Data Model Source
Scalable

5
http://db-engines.com/en/ranking
What is MongoDB?

MongoDB: A document database


MongoDB is an open-source general purpose document database.

Instead of storing data in rows and columns as one would with a relational database, MongoDB
uses a document data model and stores a binary format of JSON documents called BSON.

Documents contain one or more fields, and each field contains a value of specific data type,
including arrays and binary data. Documents are stored in collections, and collections are
stored in databases.

There is no fixed schemas in MongoDB, so documents can vary in structure and can be
adapted dynamically.

It may be helpful to think of documents as roughly equivalent to rows in a relational database;


fields as equivalent to columns; and collections as tables.

6
What is MongoDB?

MongoDB: A document database

RDBMS MongoDB

Database Database

Table Collection

Index Index

Row Document

Column Field

Join Embedding & Linking & $lookup

7
What is MongoDB?

MongoDB: A document database

8
What is MongoDB?

MongoDB: A document database


A record in MongoDB is a document, which is a data structure composed of field and value
pairs. MongoDB documents are similar to JSON objects. The values of fields may include
other documents, arrays, and arrays of documents.

9
What is MongoDB?

MongoDB: A document database

10
What is MongoDB?

11
What is MongoDB?

MongoDB: A document database


Relational MongoDB
{
first_name: ‘Paul’,
surname: ‘Miller’,
city: ‘London’,
location: [45.123,47.232],
cars: [
{ model: ‘Bentley’,
year: 1973,
value: 100000, … },
{ model: ‘Rolls Royce’,
year: 1965,
value: 330000, … }
]
}

12
What is MongoDB?

MongoDB: A document database


Relational Schema

13
What is MongoDB?

MongoDB: A document database


MongoDB

14
What is MongoDB?

MongoDB: A document database

{
_id: “123”,
title: "MongoDB: The Definitive Guide",
authors: [
{ _id: "kchodorow", name: "Kristina Chodorow“ },
{ _id: "mdirold", name: “Mike Dirolf“ }
],
published_date: ISODate(”2010-09-24”),
pages: 216,
language: "English",
publisher: {
name: "O’Reilly Media",
founded: 1980,
locations: ["CA”, ”NY” ]
}
}

15
What is MongoDB?

MongoDB: A document database


MongoDB is an open-source general purpose document database.
MongoDB also features a rich query language; atomic update modifiers; text search;
the Aggregation Framework for analytics similar to SQL GROUP BY operations; and
Map-Reduce for complex, in-place analysis.
MongoDB provides full index support, including secondary, compound and
geospatial indexes.
MongoDB also provides native, idiomatic drivers for all popular programming
languages and frameworks to make development natural.
Built-in replication with automated failover provides high availability. Auto-sharding
enables horizontal scaling for large deployments.

16
What is MongoDB?

MongoDB: A document database

https://docs.mongodb.com/
17
What is MongoDB?

JSON: Javascript Object Notation

MongoDB uses JSON (or rather, BSON, but we’ll get to that) to deal with structured
documents.

18
What is MongoDB?

JSON: Javascript Object Notation


But what is JSON exactly? Take a look at the following simple example:

If you’re a JavaScript developer, this should look familiar.


A JSON document can have one or more key: value pairs. The keys are always
strings and must always be quoted (unlike in JavaScript, where quotes are
sometimes optional).

19
What is MongoDB?

JSON: Javascript Object Notation


And the value can be one of the following types:
1. Number (double-precision floating-point format, depending on implementation)
2. String (double-quoted Unicode, with backslash escaping)
3. Boolean (true or false)
4. Array (an ordered, comma-separated sequence of values enclosed in square
brackets)
5. Object (an unordered, comma-separated collection of key: valuepairs enclosed
in curly braces)
6. null (empty)

20
What is MongoDB?

BSON: Binary JSON


Internally MongoDB stores its data in BSON, or “Binary JSON”, format.
You don’t really need to know anything about BSON to work with MongoDB so feel
free to skip this section but if you’re, like me, wondering about performance and “how
stuff works” keep on reading.
Why BSON? One of the reasons is because it’s fast scannable.
Considering JSON is just a string, in order to find a specific key you need to scan
every single character in that string, keeping track of the level of nesting, until you
found that specific key. That could be tons of data that needs to be scanned.
BSON, however, stores the length of values so in order to find that specific key you
can just skip past values and read the next key.
http://bsonspec.org/

21
What is MongoDB?

BSON: Binary JSON

22
What is MongoDB?

BSON: Binary JSON


A BSON document starts with the document length, in this case 23 bytes. Lengths
are stored as 32-bit integers, little endian, so “23” would actually be stored
as \x17\x00\x00\x00.
While for readability I wrote “23”, note lengths are 4 bytes long since they are 32-bit
integers.
Then for each key: value pair BSON specifies the value type as a single byte, key
as null terminated string, value length as 32-bit integer if applicable and the value
itself.
Documents, arrays and strings are null terminated.

23
What is MongoDB?

ObjectId
ObjectId is a special 12 byte value
Guaranteed to be unique across your cluster

24

You might also like