Dev Jumpstart: Building Your First App

  • Published on
    25-Jul-2015

  • View
    171

  • Download
    0

Transcript

1. Building Your First App With MongoDB Andrew Erlichson, Vice President of Engineering Developer Experience 2. What is MongoDB 3. 4 Document Database Not for .PDF & .DOC files A document is essentially an associative array Document == JSON object Document == PHP Array Document == Python Dict Document == Ruby Hash etc 4. 5 Terminology RDBMS MongoDB Table, View Collection Row Document Index Index Join Embedded Document Foreign Key Reference Partition Shard 5. 6 Open Source MongoDB is an open source project https://www.github.com/mongodb Started & sponsored by MongoDB, Inc. Licensed under the AGPL Commercial licenses available Contributions welcome 6. 7 Horizontally Scalable 7. 8 Database Landscape 8. 9 Full Featured Ad Hoc queries Real time aggregation Rich query capabilities Geospatial features Support for most programming languages Flexible schema 9. 10 http://www.mongodb.org/downloads 1 2 10. 11 11. Andrews-MacBook:Downloads aje$ tar xvf mongodb-osx-x86_64-3.0.3.tgz x mongodb-osx-x86_64-3.0.3/README x mongodb-osx-x86_64-3.0.3/THIRD-PARTY-NOTICES x mongodb-osx-x86_64-3.0.3/GNU-AGPL-3.0 x mongodb-osx-x86_64-3.0.3/bin/mongodump x mongodb-osx-x86_64-3.0.3/bin/mongorestore x mongodb-osx-x86_64-3.0.3/bin/mongoexport x mongodb-osx-x86_64-3.0.3/bin/mongoimport x mongodb-osx-x86_64-3.0.3/bin/mongostat x mongodb-osx-x86_64-3.0.3/bin/mongotop x mongodb-osx-x86_64-3.0.3/bin/bsondump x mongodb-osx-x86_64-3.0.3/bin/mongofiles x mongodb-osx-x86_64-3.0.3/bin/mongooplog x mongodb-osx-x86_64-3.0.3/bin/mongoperf x mongodb-osx-x86_64-3.0.3/bin/mongosniff x mongodb-osx-x86_64-3.0.3/bin/mongod x mongodb-osx-x86_64-3.0.3/bin/mongos x mongodb-osx-x86_64-3.0.3/bin/mongo Unpacking the Tarball 12. $ cd mongodb-osx-x86_64-3.0.3/bin $ mkdir p /data/db $ ./mongod Running MongoDB 13. aje-desktop:bin aje$ ./mongod 2015-05-28T09:45:41.621-0400 I JOURNAL [initandlisten] journal dir=/data/db 2015-05-28T09:45:41.621-0400 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed 2015-05-28T09:45:41.638-0400 I JOURNAL [durability] Durability thread started 2015-05-28T09:45:41.638-0400 I CONTROL [initandlisten] MongoDB starting : pid=17522 port=27017 64-bit host=aje- desktop 2015-05-28T09:45:41.638-0400 I JOURNAL [journal writer] Journal writer thread started 2015-05-28T09:45:41.638-0400 I CONTROL [initandlisten] db version v3.0.3 2015-05-28T09:45:41.638-0400 I CONTROL [initandlisten] git version: b40106b36eecd1b4407eb1ad1af6bc60593c6105 2015-05-28T09:45:41.638-0400 I CONTROL [initandlisten] build info: Darwin bs-osx108-7 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29 13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49 2015-05-28T09:45:41.638-0400 I CONTROL [initandlisten] allocator: system 2015-05-28T09:45:41.638-0400 I CONTROL [initandlisten] options: {} 2015-05-28T09:45:41.647-0400 I NETWORK [initandlisten] waiting for connections on port 27017 Log Output from mongod 14. aje-desktop:bin aje$ ./mongo MongoDB shell version: 3.0.3 connecting to: 127.0.0.1:7=27017/test > db.names.insert({'fullname':'Andrew Erlichson}) WriteResult({ "nInserted" : 1 }) > db.names.findOne() { "_id" : ObjectId("55671da150a222c93b33bca7"), "fullname" : "Andrew Erlichson", } > Inserting Your First Document 15. 16 Web Demo MongoDB Python Bottle web framework Pymongo 16. $ sudo easy_install pip $ sudo pip install pymongo $ sudo pip install Python Prerequisites 17. from pymongo import MongoClient from bottle import route, run, template @route('/') def index(): collection = db.names doc = collection.find_one() return "Hello " + doc['fullname'] client = MongoClient('localhost', 27017) db = client.test run(host='localhost', port=8080) hello.py 18. @route('/') def index(): collection = db.names doc = collection.find_one() return "Hello " + doc['fullname] client = MongoClient('localhost', 27017) db = client.test run(host='localhost', port=8080) hello.py Import the modules needed for Pymongo and the bottle web framework 19. from pymongo import MongoClient from bottle import route, run, template @route('/') def index(): collection = db.names doc = collection.find_one() return "Hello " + doc['fullname'] run(host='localhost', port=8080) hello.py Connect to the MongoDB Database on Localhost and use the test database 20. from pymongo import MongoClient from bottle import route, run, template @route('/') def index(): collection = db.names doc = collection.find_one() return "Hello " + doc['fullname'] client = MongoClient('localhost', 27017) db = client.test run(host='localhost', port=8080) hello.py Define a handler that runs when user hits the root of our web servers. That handler does a single query to the database and prints back to the web browser 21. from pymongo import MongoClient from bottle import route, run, template @route('/') def index(): collection = db.names doc = collection.find_one() return "Hello " + doc['fullname'] client = MongoClient('localhost', 27017) db = client.test hello.py Start the webserver on locahost, listening on port 8080 22. Our First App 23. Lets Design a Blog 24. Determine Your Entities First Step In Your App 25. 26 Entities in our Blogging System Users (post authors) Posts Comments Tags 26. We Would Start By Doing Schema Design In a relational based app 27. 28 Typical (relational) ERD tag_id tag tags post_id post_title body post_date post_author_uid post_id comment_id comment author_name comment_date author_email uid username password Email post_id tag_id users posts comments post_tags 28. In MongoDB We Start By Building Our App And Let The Schema Evolve 29. 30 MongoDB ERD title body date username Posts [ ] comments [ ] tags Username password email Users 30. Manipulating Blog Data (mongo shell version) 31. user = { _id: erlichson', "password" : "a7cf1c46861b140894e1371a0eb6cd6791ca2e339f1a8d83a1846f6c811 41dec,zYJue", , email: andrew@mongodb.com', } Start with an object (or array, hash, dict, etc) 32. > db.users.insert(user) Insert the record No collection creation needed 33. > db.users.findOne() { "_id" : "erlichson", "password" : "a7cf1c46861b140894e1371a0eb6cd6791ca2e339f1a8d83a1846f6c81141dec,zYJue", "email" : aje@10gen.com } Querying for the user 34. > db.posts.insert({ title: Hello World, body: This is my first blog post, date: new Date(2013-06-20), username: erlichson, tags: [adventure, mongodb], comments: [] }) Creating a blog post 35. db.posts.find().pretty() "_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : "erlichson", "tags" : [ "adventure", "mongodb" ], "comments" : [ ] } Finding the Post 36. > db.posts.find({tags:'adventure'}).pretty() { "_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : "erlichson", "tags" : [ "adventure", "mongodb" ], "comments" : [ ] } Querying an Array 37. > db.posts.update({_id: new ObjectId("51c3bcddfbd5d7261b4cdb5b")}, {$push:{comments: {name: 'Steve Blank', comment: 'Awesome Post'}}}) > Using Update to Add a Comment 41. MongoDB Drivers 42. 43 43. Next Steps 44. 46 http://docs.mongodb.org/ecosystem/drivers/