|
| 1 | +--- |
| 2 | +title: "MongoDB" |
| 3 | +tags: mongodb database nosql |
| 4 | +--- |
| 5 | + |
| 6 | +Structure |
| 7 | +- |
| 8 | + |
| 9 | +RDBMS | MongoDB |
| 10 | +-----------|--------- |
| 11 | +Database | Database |
| 12 | +Table | Collection |
| 13 | +Row | Document |
| 14 | +Column | Field |
| 15 | +Table Join | Embedded Document |
| 16 | +Primary Key| ObjectId |
| 17 | + |
| 18 | + |
| 19 | +ObjectId is a 12 byte binary BSON type, composed of |
| 20 | + |
| 21 | +Size | Description |
| 22 | +-------| ----------- |
| 23 | +4 bytes| Unix timestamp (in second) |
| 24 | +3 bytes| Client machine ID |
| 25 | +2 bytes| Client process ID |
| 26 | +3 bytes| Incremented Counter, starting with a random value |
| 27 | + |
| 28 | +Advantages over RDBMS |
| 29 | +- |
| 30 | +* Schema less: Number of fields, content and size of the document can differ from one document to another |
| 31 | +* No complex joins |
| 32 | +* Ease of scale-out |
| 33 | + |
| 34 | +## Designing Schema in MongoDB |
| 35 | + |
| 36 | +- Design schema according to user requirements. |
| 37 | +- Combine objects into one document if you will use them together. Otherwise separate them (but make sure there should not be need of joins). |
| 38 | +- Duplicate the data (but limited) because disk space is cheap as compare to compute time. |
| 39 | +- Do joins while write, not on read. |
| 40 | +- Optimize your schema for most frequent use cases. |
| 41 | +- Do complex aggregation in the schema. |
| 42 | + |
| 43 | +## Create Collection |
| 44 | +- `db.createCollection(name, options)` |
| 45 | +- option `capped`. Capped Collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size |
| 46 | +- `size` (in bytes) and `max` (number of documents) in the capped collection. |
| 47 | + |
| 48 | +## Data types |
| 49 | +- Min/ Max keys − This type is used to compare a value against the lowest and highest BSON elements. |
| 50 | + |
| 51 | +## Replica |
| 52 | + |
| 53 | +MongoDB can provide high availability with replica sets. |
| 54 | + |
| 55 | +A replica set consists of two or more mongo DB instances. Each replica set member may act in the role of the primary or secondary replica at any time. |
| 56 | + |
| 57 | +The primary replica is the main server which interacts with the client and performs all the read/write operations. |
| 58 | + |
| 59 | +The Secondary replicas maintain a copy of the data of the primary using built-in replication. |
| 60 | + |
| 61 | +When a primary replica fails, the replica set automatically switches over to the secondary and then it becomes the primary server. |
| 62 | + |
| 63 | + |
| 64 | +## Sharding |
| 65 | + |
| 66 | +Sharding is a concept in MongoDB, which splits large data sets into small data sets across multiple MongoDB instances. |
| 67 | + |
| 68 | +It is a MongoDB approach to meet the demands of data growth. It is the horizontal partition of data in a database or search engine. Each partition is referred as shard or database shard. |
| 69 | + |
| 70 | +Components of Shard: |
| 71 | + |
| 72 | +- A Shard: a MongoDB instance which holds subset of the data |
| 73 | +- Config server: a MongoDB instance which holds metadata about cluster |
| 74 | +- A router: a MongoDB which redirect the commands send by client to the right server |
| 75 | + |
| 76 | +Indexing in MongoDB |
| 77 | + |
| 78 | +# References |
| 79 | + |
| 80 | +- [Top 20 MongoDB Interview Questions & Answers](https://www.guru99.com/mongodb-interview-questions.html) |
| 81 | +- [MongoDB Sharding](https://www.guru99.com/mongodb-sharding-implementation.html) |
| 82 | +- [MongoDB - Advantages](https://www.tutorialspoint.com/mongodb/mongodb_advantages.htm) |
| 83 | + |
| 84 | +# Read more |
0 commit comments