Last active
December 23, 2015 12:39
-
-
Save tairov/6636969 to your computer and use it in GitHub Desktop.
fan out on write with bucketing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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