By Josh Fermin
Storing data across multiple nodes/machines.
A single machine may run out of storage or have bottlenecked reads and writes
Sharding is horizontally scalable - Add machines as demand increases.
High throughput and large amounts data put a lot of stress on a single server.
Set sizes larger than RAM stress I/O capacity
High query rates can exhuast CPU capacity of a single server.
Sharding happens on a collection level.
Sharding divides a collection's data by a shard key
Two types of partitioning: range based and hash based
Taken from Mongo Docs.
mkdir /data/configdb
mongod --configsvr --dbpath /data/configdb
mongos --configdb localhost:27019 --port 27017
mkdir /data/shard1
mongod --dbpath /data/shard1 --port 27018 --shardsvr
mkdir /data/shard2
mongod --dbpath /data/shard2 --port 27020 --shardsvr
Note different port
mongo --port 27017
mongos> sh.addShard("localhost:27018")
{ "shardAdded": "shard0001", "ok" : 1 }
mongos> sh.addShard("localhost:27020")
{ "shardAdded": "shard0002", "ok" : 1 }
First create a database and a collection:
mongos> use testDB
mongos> db.createCollection(testCollection)
Then enable sharding on each:
Second param is choosing the shard key as md5 with hash partitioning. For range based do: "md5":1mongos> sh.enableSharding(“testDB”)
mongos> sh.shardCollection(“testDB.testCollection”, { “md5”:”hashed” })
GridFS is an extension on top of mongo
Allows you to store/retrieve files that exceed BSON doc limit of 16mb
Instead of one doc per file, GridFS breaks it into 255k chunks.
Each chunk becomes a separate document.
When you query for a file, the driver or client will reassemble it as needed.
GridFS uses two collections to store files.
Two ways:
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
MongoClient client = new MongoClient("mongodb://localhost:27017");
MongoServer server = client.GetServer();
MongoDatabase database = server.GetDatabase("testDB");
MongoCollection testCollection = database.GetCollection("testCollection");
MongoGridFS gfs = new MongoGridFS(database);
gfs.Upload(@"C:\test.txt");
After uploading, the chunks and files collections will be created.