MongoDB sharding test

  • 2020-06-01 11:14:25
  • OfStack

Sharding is one way of extending mongoDB. Shards split 1 collection and store different parts on different machines. When the collections of a database is too large for the current space, you need to add a new machine. Sharding will automatically distribute collection data to the new server.

1. Connect to mongos to view information related to the system

configsvr> show dbs
configsvr> use config
configsvr> show collections
onfigsvr> db.mongos.find()
{ "_id" :"racdb:28885", "ping" :ISODate("2016-03-21T09:23:05.106Z"), "up" :NumberLong(1436), "waiting" : true, "mongoVersion" :"3.2.3" }
{ "_id" :"host8.localdomain:28885", "ping" :ISODate("2016-03-21T09:23:07.960Z"), "up" :NumberLong(1427), "waiting" : true, "mongoVersion" :"3.2.3" }
{ "_id" :"host9.localdomain:28885", "ping" :ISODate("2016-03-21T09:23:03.521Z"), "up" :NumberLong(1407), "waiting" : true, "mongoVersion" :"3.2.3" }
configsvr> db.shards.find()
{ "_id" : "shard1","host" : "shard1/host8:28017,racdb:28017" }
{ "_id" : "shard2","host" : "shard2/host8:28018,racdb:28018" }
configsvr> db.databases.find()
{ "_id" :"im_offline_msg", "primary" : "shard1","partitioned" : true }
{ "_id" : "testdb","primary" : "shard2", "partitioned" : true }
{ "_id" : "test","primary" : "shard1", "partitioned" : true }
{ "_id" : "blogdb","primary" : "shard2", "partitioned" : false }

2. Enable sharding for the database

2.1 sharding of database or collection (without sharding) can be currently connected to mongos:

mongos> db.stats()

2.2 sharding function for database activation:

# mongo racdb:28885
# or 
# mongo racdb:28885
mongos> use admin
mongos> db.runCommand( { enableSharding:"blogdb"} )

2.3 when you look at the database partitioning situation, partitioned becomes "true".

configsvr> use config
switched to db config
configsvr> db.databases.find()
{ "_id" :"im_offline_msg", "primary" : "shard1","partitioned" : true }
{ "_id" : "testdb","primary" : "shard2", "partitioned" : true }
{ "_id" : "test","primary" : "shard1", "partitioned" : true }
{ "_id" : "blogdb","primary" : "shard2", "partitioned" : true }

Enabling database sharding does not separate the data; you also need to shard collection.

3. Enable sharding for collections

There are several issues to consider before starting:

Select which key column to use as shard key. (more information: Considerations for Selecting Shard Keys)

If data already exists in the collection, the key column selected as shard key must be indexed; If the collection is empty, mongodb creates an index when the collection sharding (sh.shardCollection) is activated.

Set sharding function sh.shardCollection,


mongos > sh.shardCollection("", { "_id": "hashed"})

Test insert data:

-- use python The command 
# create python file 
$ vi
#-*- coding: UTF-8 -*-
import pymongo
client = pymongo.MongoClient("racdb", 28885)
db = client.testdb
# To view testdb Collection information in the database 
print (db.collection_names())
# Connect to the my_collection A collection of 
print (db.my_collection)
# empty my_collection Collection document information 
# According to my_collection The number of documents in the collection 
print (db.my_collection.find().count())
# insert 10000 Bar document information 
for i in range(10000):
# According to my_collection The number of documents in the collection 
print (' After insertion, the current number of documents: ')
print (db.my_collection.find().count())
# An insert 
[mongod@racdb ~]$
[u'system.indexes', u'table1',u'my_collection']
Collection(Database(MongoClient(host=['racdb:28885'],document_class=dict, tz_aware=False, connect=True), u'testdb'), u'my_collection')

After insertion, the current number of documents:

# Or use mongo shell Insert test data 
for (var i=1; i<=100000; i++) {{"id": i,"myName" : "cc"+i, "myDate" : new Date()});

Enable collection sharding

mongos> show collections
mongos>{"id": "hashed" })
mongos>sh.shardCollection("", { "id": "hashed"})
mongos> db.stats()
-- To view sharding  state 
mongos> db.printShardingStatus();

The above content is the MongoDB sharding test introduced to you by this site, I hope to help you!

Related articles: