// 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)
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
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.
https://www.mongodb.com/compare/mongodb-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).
- read-only
- queryable object
- contents are pre-defined by an aggregation pipeline or other collections or other views.
- 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
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.
To check if your MongoDB instance is using Wired Tiger:
db.serverStatus().storageEngine
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.
-
- a replica set or sharded cluster, NOT SUPPORT stand alone instance
-
- a storage engine, supporting document-level locking. (WiredTiger)
-
- Write operation with Write Concern of 1.
// common
deleteOne()
deleteMany()
remove()
// additional
findOneAndDelete()
findAndModify()
bulkWrite()
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()