Database Reference
In-Depth Information
"ns" : "cloud-docs.spreadsheets",
"key" : {
"_id" : 1
},
"v" : 0
},
{
"ns" : "cloud-docs.spreadsheets",
"key" : {
"username" : 1,
"_id" : 1
},
"name" : "username_1__id_1",
"v" : 0
}
]
Once you've sharded the collection, then at last, sharding is ready to go. You can now
write to the cluster and data will distribute. We'll see how that works in the next section.
9.2.2
Writing to a sharded cluster
We'll write to the sharded collection so that you can observe the formation and move-
ment of chunks, which is the essence of MongoDB's sharding. The sample docu-
ments, each representing a single spreadsheet, will look like this:
{
_id: ObjectId("4d6f29c0e4ef0123afdacaeb"),
filename: "sheet-1",
updated_at: new Date(),
username: "banks",
data: "RAW DATA"
}
Note that the data field will contain a 5 KB string to simulate the raw data.
This topic's source code includes a Ruby script you can use to write documents to
the cluster. The script takes a number of iterations as its argument, and for each itera-
tion, it inserts one 5 KB document for each of 200 users. The script's source is here:
require 'rubygems'
require 'mongo'
require 'names'
@con = Mongo::Connection.new("localhost", 40000)
@col = @con['cloud']['spreadsheets']
@data = "abcde" * 1000
def write_user_docs(iterations=0, name_count=200)
iterations.times do |n|
name_count.times do |n|
doc = { :filename => "sheet-#{n}",
:updated_at => Time.now.utc,
:username => Names::LIST[n],
:data => @data
}
Search WWH ::




Custom Search