SlideShare a Scribd company logo
MongoDB + node.js で作るソーシャルゲーム
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB




                                         HTTP
                                       WebSocket




     Client (Flash)                       LB

                                                    node.js
                                                                             3x3 Sharding
                                                                              MongoDB



                                                               Content
                                                   SVN or git Distribution
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB



                        var entity = {
                          _id: ‘abc’,
                          name: ‘test’,
                          body: {
                            parts: {
                              face: 0, eye: 2, nose: 4, mouth: 2
                            },
                            colors: {
                               skin: 1, eye: 4, hair: 3
                            }
                          }
                        };

                        col.save(entity);

                        col.findOne({_id:‘abc’}, function(err, result) {
                            console.log(result.body.parts);
                        });

                        col.update({_id:‘abc’},
                          {‘$set’, {name: ‘modified’}}
                        );
Mongo Tokyo 2011 - node.js + MongoDB




                        https://github.com/orlandov/node-mongodb/




                        https://github.com/christkv/node-mongodb-native/
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB




                var client = new Db('test', new Server("127.0.0.1", 27017, {}));
                client.open(function(err, p_client) {
                  client.createCollection('test_insert',function(err, collection) {
                    client.collection('test_insert', function(err, collection) {
                      for(var i = 1; i < 1000; i++) {
                        collection.insert({c:1}, function(err, docs) {});
                      }
                      collection.insert({a:2}, function(err, docs) {
                        collection.insert({a:3}, function(err, docs) {
                          collection.count(function(err, count) {
                            test.assertEquals(1001, count);
                            collection.find(function(err, cursor) {
                              cursor.toArray(function(err, results) {
                                test.assertEquals(1001, results.length);
                                test.assertTrue(results[0] != null);
                                client.close();
                              });
                            });
                          });
                        });
                      });
                    });
                  });
                });
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB




                var db = new Database(‘127.0.0.1’, 3306, ‘test’);
                db.open();

                var item = db.collection(‘item’);

                item
                  .begin()
                  .insert({_id:‘test’, name:‘abcdefg’})
                  .insert({_id:‘test2’, name:‘123456’, values:[0,1,2,3]);
                  .update({_id:‘test’}, {‘$set’: { name: ‘hijklmn’ })
                  .find({}).each(function(entity) {
                     console.log(entity.name);
                  })
                  .success(function() {
                     console.log(‘done’);
                  })
                  .failure(function(err) {
                     console.log(‘error’, err);
                  })
                  .end();
Mongo Tokyo 2011 - node.js + MongoDB




                        https://github.com/LearnBoost/mongoose
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB




                var spec1 = {
                   name: ‘test’,
                   type: ‘text’,
                   label: ‘label-1’,
                   class: ‘number’,
                   min: 0, max: 100
                };

                var spec2 = {
                   name: ‘test2’,
                   type: ‘group’,
                   label: ‘group-1’,
                   fields: [spec3, spec4, spec5]
                };

                var formSpec = [
                   spec1, spec2
                ];

                showForm(formSpec);
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB
Mongo Tokyo 2011 - node.js + MongoDB

                                 { "_id" : "plant_ichigo",

                                       "name" : "    ",
                                       "type" : "plant",
                                       "plant" : {
                                         "tags" : [ "vegetable" ],
                                         "time" : { "grow" : 10, "expire" : 60 },
                                         "rewards" : [
                                 !       {
                                 !       !    "type" : "coin",
                                 !       !    "rate" : 1,
                                 !       !    "min" : 5,
                                 !       !    "max" : 20,
                                 !       !    "code" : ""
                                 !       },
                                 !       {
                                 !       !    "type" : "exp",
                                 !       !    "rate" : 1,
                                 !       !    "min" : 1,
                                 !       !    "max" : 2,
                                 !       !    "code" : ""
                                 !       },
                                 !       {
                                 !       !    "type" : "item",
                                 !       !    "rate" : 1,
                                 !       !    "min" : 1,
                                 !       !    "max" : 1,
                                 !       !    "code" : "material_ichigo"
                                 !       }
                                 ]}
Mongo Tokyo 2011 - node.js + MongoDB

More Related Content

MongoDB + node.js で作るソーシャルゲーム

  • 2. Mongo Tokyo 2011 - node.js + MongoDB
  • 3. Mongo Tokyo 2011 - node.js + MongoDB
  • 4. Mongo Tokyo 2011 - node.js + MongoDB
  • 5. Mongo Tokyo 2011 - node.js + MongoDB
  • 6. Mongo Tokyo 2011 - node.js + MongoDB
  • 7. Mongo Tokyo 2011 - node.js + MongoDB
  • 8. Mongo Tokyo 2011 - node.js + MongoDB
  • 9. Mongo Tokyo 2011 - node.js + MongoDB HTTP WebSocket Client (Flash) LB node.js 3x3 Sharding MongoDB Content SVN or git Distribution
  • 10. Mongo Tokyo 2011 - node.js + MongoDB
  • 11. Mongo Tokyo 2011 - node.js + MongoDB var entity = { _id: ‘abc’, name: ‘test’, body: { parts: { face: 0, eye: 2, nose: 4, mouth: 2 }, colors: { skin: 1, eye: 4, hair: 3 } } }; col.save(entity); col.findOne({_id:‘abc’}, function(err, result) { console.log(result.body.parts); }); col.update({_id:‘abc’}, {‘$set’, {name: ‘modified’}} );
  • 12. Mongo Tokyo 2011 - node.js + MongoDB https://github.com/orlandov/node-mongodb/ https://github.com/christkv/node-mongodb-native/
  • 13. Mongo Tokyo 2011 - node.js + MongoDB
  • 14. Mongo Tokyo 2011 - node.js + MongoDB var client = new Db('test', new Server("127.0.0.1", 27017, {})); client.open(function(err, p_client) { client.createCollection('test_insert',function(err, collection) { client.collection('test_insert', function(err, collection) { for(var i = 1; i < 1000; i++) { collection.insert({c:1}, function(err, docs) {}); } collection.insert({a:2}, function(err, docs) { collection.insert({a:3}, function(err, docs) { collection.count(function(err, count) { test.assertEquals(1001, count); collection.find(function(err, cursor) { cursor.toArray(function(err, results) { test.assertEquals(1001, results.length); test.assertTrue(results[0] != null); client.close(); }); }); }); }); }); }); }); });
  • 15. Mongo Tokyo 2011 - node.js + MongoDB
  • 16. Mongo Tokyo 2011 - node.js + MongoDB var db = new Database(‘127.0.0.1’, 3306, ‘test’); db.open(); var item = db.collection(‘item’); item .begin() .insert({_id:‘test’, name:‘abcdefg’}) .insert({_id:‘test2’, name:‘123456’, values:[0,1,2,3]); .update({_id:‘test’}, {‘$set’: { name: ‘hijklmn’ }) .find({}).each(function(entity) { console.log(entity.name); }) .success(function() { console.log(‘done’); }) .failure(function(err) { console.log(‘error’, err); }) .end();
  • 17. Mongo Tokyo 2011 - node.js + MongoDB https://github.com/LearnBoost/mongoose
  • 18. Mongo Tokyo 2011 - node.js + MongoDB
  • 19. Mongo Tokyo 2011 - node.js + MongoDB
  • 20. Mongo Tokyo 2011 - node.js + MongoDB
  • 21. Mongo Tokyo 2011 - node.js + MongoDB var spec1 = { name: ‘test’, type: ‘text’, label: ‘label-1’, class: ‘number’, min: 0, max: 100 }; var spec2 = { name: ‘test2’, type: ‘group’, label: ‘group-1’, fields: [spec3, spec4, spec5] }; var formSpec = [ spec1, spec2 ]; showForm(formSpec);
  • 22. Mongo Tokyo 2011 - node.js + MongoDB
  • 23. Mongo Tokyo 2011 - node.js + MongoDB
  • 24. Mongo Tokyo 2011 - node.js + MongoDB { "_id" : "plant_ichigo", "name" : " ", "type" : "plant", "plant" : { "tags" : [ "vegetable" ], "time" : { "grow" : 10, "expire" : 60 }, "rewards" : [ ! { ! ! "type" : "coin", ! ! "rate" : 1, ! ! "min" : 5, ! ! "max" : 20, ! ! "code" : "" ! }, ! { ! ! "type" : "exp", ! ! "rate" : 1, ! ! "min" : 1, ! ! "max" : 2, ! ! "code" : "" ! }, ! { ! ! "type" : "item", ! ! "rate" : 1, ! ! "min" : 1, ! ! "max" : 1, ! ! "code" : "material_ichigo" ! } ]}
  • 25. Mongo Tokyo 2011 - node.js + MongoDB