We usually need to store big text data into MongoDB. After sharding, the best storage method is to allow dB sharding and specify the sharding method (shard key) of the collection ). This operation can be easily completed using MongoDB shell, but when we use python to import data, we must use pymongo. The following is an example of pymongo allowing sharding.
#! /Bin/ENV pythonimport pydomainif _ name _ = '_ main _': conn = pymongo. connection ('2017. 168.1.1 ', 27017) DB = conn ['test'] # assume that the DB named test already exists db_admin = conn ['admin'] # The command must be executed through the DB named admin before col_data = DB ["data"] For I in range (1, 50): col_data.insert ({'_ id': I, 'value' :( I * 200)}) # insert test data. The Shard key must exist before partitioning, in this example, _ id db_admin.command ('enablessharding', 'test') # Check whether db_admin.command ('shardcollect', 'test. data ', key = {' _ id': 1}) # specify the target collection and the corresponding shard key Conn. close () Pass
In addition, multiple shard keys can be specified, which is similar to creating a composite index:
Db_admin.command ('shardcollect', 'test. data', key = {'_ id': 1, 'A': 1,' B ': 1 })