NOSQL (MongoDB)

Introduction

  • It is a document-oriented database, all of the data is in document format

  • It supports JSON data model with dynamic schemas

CRUD Operation

Create

db.inventory.insertOne(
   { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)

db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
   { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
   { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])

Read

Update

Delete

Bulk Write

  • Perform bunch of operation in one query

Atomicity and Concurrency

  • In MongoDB, a write operation is atomicarrow-up-right on the level of a single document, even if the operation modifies multiple embedded documents within a single document.

  • When a single write operation (e.g. db.collection.updateMany()arrow-up-right) modifies multiple documents, the modification of each document is atomic, but the operation as a whole is not atomic.

  • MongoDB allows multiple clients to read and write the same data. To ensure consistency, MongoDB uses locking and concurrency control to prevent clients from modifying the same data simultaneously.

Aggregation

  • Aggregation operations process multiple documents and return computed results. You can use aggregation operations to:

  • Group values from multiple documents together.

  • Perform operations on the grouped data to return a single result.

  • Analyze data changes over time.

Data Model

  • There are 2 types of data model that can be designed

Embed data model

  • Optimizing the performance of retrieving data

  • Convenient to retrieve data by single operation

  • No convenient to update the embed document if the same data happens on multiple collections

Reference

  • Convenient to keep data be consistent

  • Using lookup to join the collections

ACID Transaction

  • As multiple-document operation is not atomic, for some situation, such as bank transfer, in order to keep data consistent, ACID transaction can be used

Last updated

Was this helpful?