Points of MongoDb

NoSQL database

MongoDb is a NoSQL database which is much more agile and flexible than relational databases.

NoSQL vs SQL:

  • Agile and flexible: schema is not required, and easy to change. Which usually cause better performance
  • Ability to scale-out (distribute the load among multiple servers)
  • To handle large volumes of structured, semi-structured, and unstructured data

Ref: SQL vs NoSQL: The Differences

Terminologies

Document: MongoDb stores data as BSON documents
Collection: analogous to an SQL table, to store a collection of documents

CRUD

Create

SQL

1
2
3
4
5
6
7
8
INSERT INTO book (
`ISBN`, `title`, `author`
)
VALUES (
'9780992461256',
'Full Stack JavaScript',
'Colin Ihrig & Adam Bretz'
);

MongoDb

1
2
3
4
5
db.book.insert({
ISBN: "9780992461256",
title: "Full Stack JavaScript",
author: "Colin Ihrig & Adam Bretz"
});

Read

SQL

1
2
SELECT title FROM book
WHERE price > 10;

MongoDb

1
2
3
4
db.book.find(
{ price: { >: 10 } },
{ _id: 0, title: 1 }
);

Update

SQL

1
2
3
UPDATE book
SET price = 19.99
WHERE ISBN = '9780992461256'

MongoDb

1
2
3
4
db.book.update(
{ ISBN: '9780992461256' },
{ $set: { price: 19.99 } }
);

Delete

SQL

1
2
DELETE FROM book
WHERE publisher_id = 'SP001';

MongoDb

1
2
3
db.book.remove({
"publisher.name": "SitePoint"
});