Skip to content

Instantly share code, notes, and snippets.

@tairov
Last active December 23, 2015 12:39
Show Gist options
  • Select an option

  • Save tairov/6636969 to your computer and use it in GitHub Desktop.

Select an option

Save tairov/6636969 to your computer and use it in GitHub Desktop.
fan out on write with bucketing
//Fan out on Write
//H Shard on “owner I sequence"
db.shardCollection("myapp.inbox". { "owner": 1, "sequence":1 });
db.shardCollection("myapp.users", { "user name":1 });
msg = {
from: "Joe",
to: [ "Bob". "Jane" ],
sent: new Date().
message: "Hi!",
}
// Send a message
for( recipient in msg.to) {
sequence = db.users.findAndModity({
query: {'user_name': recipient},
update: { '$inc': { 'msg_count': 1 }},
upsert: true,
new: true }).msg_count / 50;
db.inbox.update({owner: recipient, sequence: sequence}.
{$push: { 'messages': msg } },
{upsert: true });
}
// Read my inbox
db.inbox.find({ owner: "Joe" }).sort({ sequence: -1 }).limit(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment