Wednesday 18 September 2019

What is sharding in MongoDB?

What is sharding in MongoDB?

Question: What is sharding in MongoDB?
Sharding is a method for distributing data across multiple machines and its MongoDB's approach to meeting the demands of data growth. With increase of the size of the data, a single machine may not be sufficient to store the data OR read and write data. to fix this problem there are two different methods.
Vertical Scaling: Increase the capacity of single server and increase the power.
  • Powerful CPU
  • Increase RAM
  • Increase storage

Horizontal Scaling: dividing the system dataset and load over multiple servers.
We can use multiple server for storing and read/data data.
Using multiple server is lower cost than single machine with high-end hardware but increase the complexity in infrastructure and maintenance for the deployment.

Question: What are MongoDB Query Capabilities?
  • Sorting on one/multiple fields with ascending/descending order.
  • Projection (Retrieving only specified fields from each document in the cursor, not the entire document).
  • Cursor skip() and limit() to easily implement pagination.
  • explain() will return the detail on query will help to optimize the query.
  • aggregate() function used for grouping the records. db.users.aggregate([{$match:{gender:"male"}}, { $group: { _id: null, count: { $sum: 1 } } }]);
  • $match and $unbind function.

Question: How to database details?

  "db": "bank",
  "collections": 4,
  "objects": 12,
  "avgObjSize": 245.33333333333334,
  "dataSize": 2944,
  "storageSize": 28672,
  "numExtents": 4,
  "indexes": 2,
  "indexSize": 16352,
  "fileSize": 67108864,
  "nsSizeMB": 16,
  "extentFreeList": {
    "num": 0,
    "totalSize": 0
  "dataFileVersion": {
    "major": 4,
    "minor": 22
  "ok": 1

Question: How to collection details?
  "ns": "bank.users",
  "count": 5,
  "size": 2480,
  "avgObjSize": 496,
  "numExtents": 1,
  "storageSize": 8192,
  "lastExtentSize": 8192,
  "paddingFactor": 1,
  "paddingFactorNote": "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
  "userFlags": 1,
  "capped": false,
  "nindexes": 1,
  "totalIndexSize": 8176,
  "indexSizes": {
    "_id_": 8176
  "ok": 1