MongoDB Basics - NoSQL Tutorial

20,085 views

Published on

Learn the basics about the structure of MongoDB and how to manipulate data in the database through ProdigyView.

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
20,085
On SlideShare
0
From Embeds
0
Number of Embeds
1,417
Actions
Shares
0
Downloads
652
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

MongoDB Basics - NoSQL Tutorial

  1. 1. MongoDB Basics
  2. 2. OverviewObjectiveLearn the basics about the structure of MongoDB and how tomanipulate data in the database through ProdigyView.Requirements Installed PECL Mongo Extension A Mongo Database(if none, a link is provided where to obtain a free one) Understanding of connecting to a database in ProdigyViewEstimated Time15 Minutes
  3. 3. Follow Along With Code Example1. Download a copy of the example code at www.prodigyview.com/source.2. Install the system in an environment you feel comfortable testing in.3. Proceed to examples/database/Mongo.php www.prodigyview.com
  4. 4. Start With CollectionsMongoDB uses collections for storing groups of data. Acollection can be thought as a table in a relationaldatabase. Collection Table Collections hold what are known as documents. Collection with Documents
  5. 5. Documents AttributesDocuments exist inside a collection. Documentation canalmost be thought of as rows in a sql database, exceptthey have key difference: Documents are schema-less Document used BSON Syntax The maximum size of a document is 16 megabytes Not Relational Similar to an object but only the data/variables of an object(no methods or class hierarchy) www.prodigyview.com
  6. 6. Need a Mongo Database?If you do not have a MongoDB database to play aroundwith, you can obtain one for free from these sites. https://mongolab.com/home Or https://mongohq.com/home
  7. 7. Mongo ConnectionTo begin playing with mongo, we need a mongo database toconnect too. Below you are to set up your Mongo connectionjust like connecting to a regular database. Replace the valueswith you database. 1. Set the dbtype to mongo
  8. 8. Set the ConnectionThen connection to the database just like a normalconnection. www.prodigyview.com
  9. 9. Prepare the Data To Insert When data is inserted into Mogno, the data is inserted as an array. The array key => value format will become the format in mongo. In MongoDB, embedded array of information is allowed.1. The collection name to insert into in Mongo 2. The data in array format
  10. 10. Insert The DataOn insert, if a collection does not exist, it will be created. Thatdata will be inserted in that collection and a Mongo ID will bereturned. 2. Array of Data 1. Collection Name
  11. 11. The Mongo ID The Mongo ID is a unique id automatically generated for each document created. The id is 12 byte value that is composed of several factors. 1 2 3 4 5 6 7 8 9 10 11 12 Time Machine PID Number1. A timestamp 2. The machine the id 3. The process id 4. A random or auto- was created on incrementing number
  12. 12. Batch InsertsBatch inserts are for entering multiple rows of data. Batchinserts are faster than insert a single dataset at a time whenmultiple datasets are present. Insert data at different indexes in an array www.prodigyview.com
  13. 13. Executing A Batch Insert A batch insert will return multiple id’s in array of the new documents that were created 2. Multiple arrays to insert 1. The collection name4. The array of ids for the new documents 3. Set the option for batchInsert to true www.prodigyview.com
  14. 14. Select QueriesSelect queries in Mongo are passed through as arrays also.The queries use the key => value and will return matchingresults. 1. Set where to search 2. Set the fields to be returned 3. The collection to look in 4. An array of returned results www.prodigyview.com
  15. 15. ConditionalsSelect queries can also have conditionals in the array. Theseconditionals make for more complex searches. The $or conditionalFor more information on Mongo’s Advanced Queries, visit here:http://www.mongodb.org/display/DOCS/Advanced+Queries
  16. 16. Single DocumentWe can narrow down the search by only returning a singledocument from the database. The result differs becausethis will not be array of documents but a single documentin an array. 1. Use the same select statement 2. Set findOne to true 3. Returns a single document in the format of an array
  17. 17. Updating FieldsUpdating fields use the same syntax as inserting andsearching: your data is in an array. We are going to start bycreating a new information to put in the document and searcharguments. 1. Set the documents to have this value 2. Search where key is equal to these values
  18. 18. Update the FieldsAnd to perform the update, we pass our data intoPVDatabase::updateStatement() .1. Collection Name 2. Fields to update 3. Where to update the fields www.prodigyview.com
  19. 19. WARNING When you update a document, the ENTIRE DOCUMENT WILL BE REPLACED WITH THE UDPATE VALUES. 1. Entire Document will be replaced with thisRemember when doing a regular update, add the old values withthe new values, unless conditionals are used.
  20. 20. Update With Conditionals To prevent replacing the entire document, we can update Mongo DB with conditionals. These conditionals will only change the fields that are specified. A conditional operatorFor more information on visit:http://www.mongodb.org/display/DOCS/Updating
  21. 21. DeletingThe final action in this tutorial is going to be deleting a documentfrom the database. We are going to use the same syntax as inour select statement for finding and delete the matchingdocuments. www.prodigyview.com
  22. 22. API ReferenceFor a better understanding of the database, visit the apiby clicking on the link below.PVDatabase More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com

×