Skip to content

Instantly share code, notes, and snippets.

View vhbui02's full-sized avatar

VH vhbui02

View GitHub Profile
@vhbui02
vhbui02 / mondodb-standard-views.md
Last active May 18, 2023 03:00
[MongoDB Standard Views] #mongodb
// specify 3 options - 'viewOn', 'pipeline' and 'collation'
db.createCollection(
  'some coll name here',
  {
		"viewOn": "source coll name here",
    "pipeline": [
    	//... add some agg stages here
      // NO $out, $merge
 // NO $lookup (embedded pipeline)
@vhbui02
vhbui02 / olap-vs-oltp.md
Last active May 17, 2023 07:37
[OLAP vs OLTP - design database]

https://viblo.asia/p/oltp-va-olap-co-gi-khac-nhau-maGK786BZj2#_mot-vi-du-oltp-thuc-te-3

https://dba.stackexchange.com/questions/4622/when-should-you-denormalize

OLTP

T in OLTP stands for Transactional, meaning process/operation that made a small change inside a large database system.

E.g.

  • An ATM that withdraws your money, simply subtract your balance, substract the cash in ATM, and you receive cash.
  • Texting to other people: add text message to your data model and text message to their data model.
@vhbui02
vhbui02 / sql-vs-mysql.md
Last active May 17, 2023 10:46
[MySQL vs MongoDB] #mysql #mongodb

https://www.mongodb.com/compare/mongodb-mysql

MySQL

  • relational
  • tables and rows, 1 row consists of columns
  • referential integrity (check if foreign key mapping is valid, e.g. a Student table uses course_id as a foreign key mapping to Course table, somehow this Student writes a value of course_id that does not exist in id Column in Course table, we said this is invalid reference.)
  • use SQL for data access.
  • JOIN is required to retrieve data from multiple table.
  • DB schema and data model (think of it as a design pattern) need to be defined ahead of time. This is a rigid approach to ensure data safety, but in exchange of flexibility (e.g. changing data type => schema migration occurs).
@vhbui02
vhbui02 / mongodb-views.md
Last active May 17, 2023 18:58
[MongoDB Views] #mongodb
  • read-only
  • queryable object
  • contents are pre-defined by an aggregation pipeline or other collections or other views.

Some common Use Cases

  • Create a view on a Collection of employee data, but exclude Personal Identifiable Information (PII). If the query does not concern about the PII, it can use the View.
  • Create a view on a Collection of sensor data, but add computed fields and metrics (these data are short-lived, so it does not logical to put it inside long-lived data, a View is more appropriate approach.
  • Create a view that joins two collections containing inventory and order history. This is opinionated since MongoDB Document tend to be denormalized, they will not need a join operation.

Two types of Views: Standard View and On-Demand Materialized View

@vhbui02
vhbui02 / mongodb-text-search.md
Created May 16, 2023 15:12
[MongoDB Text Search] very important concept to grasp if you want your search query achieve to its full extent #mongodb

Data is hosted on Standalone Instance (Self-managed Deployment)

Data is hosted on MongoDB Atlas

Simply use a text index and the $text operator

@vhbui02
vhbui02 / mongodb-retryable-reads.md
Created May 16, 2023 14:12
[MongoDB Retryable Reads] #mongodb

List Read operation that're retryable reads

Collection.aggregate

Collection.find

Database.aggregate.

NOTE: driver can only retry aggregation pipelines which DO NOT INCLUDE WRITE STAGE like $out

@vhbui02
vhbui02 / mongodb-storage-engine.md
Created May 16, 2023 13:51
[MongoDB Storage Engines] #mongodb

the component of the database that is responsible for managing how data is stored

REFRESH MEMORY: data is stored in-memory and on disk.

Multiple storage engines performs better for specific workloads. Choose the right WILL SIGNIFICANTLY impact the performance.

WiredTiger (defaulted)

To check if your MongoDB instance is using Wired Tiger: db.serverStatus().storageEngine

@vhbui02
vhbui02 / mongodb-retryable-writes.md
Last active May 16, 2023 14:08
[MongoDB Retryable Writes]

NOTE: All Write operations issued with a Write Concern: 0 are NOT retryable. Check the operation carefully!

MongoDB tutomatically retry CERTAIN (not all) write operations 1 retry attempt only if:

  • network errors
  • can not find a healthy primary in replica set or shard cluster.

Prerequisites

    1. a replica set or sharded cluster, NOT SUPPORT stand alone instance
    1. a storage engine, supporting document-level locking. (WiredTiger)
    1. Write operation with Write Concern of 1.
@vhbui02
vhbui02 / mongodb-d-operation.md
Last active May 16, 2023 09:12
[MongoDB D operation] #mongodb
// common
deleteOne()
deleteMany()
remove()

// additional
findOneAndDelete()
findAndModify()
bulkWrite()
@vhbui02
vhbui02 / mongodb-u-operation.md
Last active May 17, 2023 08:06
[MongoDB U operation] #mongodb

Update Method

updateOne()
updateMany()
replaceOne() // replace at most, 1 single document that match specified filter, regardless of other matched documents
// replace diff from update, that it overrides the whole document, not just 1 or 2 field.

findAndModify()
findOneAndUpdate()
findOneAndReplace()