mongodb basics - nosql tutorial
DESCRIPTION
Learn the basics about the structure of MongoDB and how to manipulate data in the database through ProdigyView.TRANSCRIPT
MongoDB Basics
OverviewObjective
Learn the basics about the structure of MongoDB and how to manipulate data in the database through ProdigyView.Requirements Installed PECL Mongo ExtensionA Mongo Database(if none, a link is provided where to
obtain a free one)Understanding of connecting to a database in ProdigyViewEstimated Time
15 Minutes
Follow Along With Code Example
1. 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
Start With CollectionsMongoDB uses collections for storing groups of data. A collection can be thought as a table in a relational database.
Collection Table
Collection with Documents
Collections hold what are known as documents.
Documents AttributesDocuments exist inside a collection. Documentation can almost be thought of as rows in a sql database, except they have key difference:Documents are schema-lessDocument used BSON SyntaxThe maximum size of a document is 16 megabytesNot RelationalSimilar to an object but only the data/variables of an
object(no methods or class hierarchy)
www.prodigyview.com
Need a Mongo Database?If you do not have a MongoDB database to play around with, you can obtain one for free from these sites.
https://mongolab.com/home
Or
https://mongohq.com/home
Mongo ConnectionTo begin playing with mongo, we need a mongo database to connect too. Below you are to set up your Mongo connection just like connecting to a regular database. Replace the values with you database.
1. Set the dbtype to mongo
Set the ConnectionThen connection to the database just like a normal connection.
www.prodigyview.com
Prepare the Data To InsertWhen 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
Insert The DataOn insert, if a collection does not exist, it will be created. That data will be inserted in that collection and a Mongo ID will be returned.
1. Collection Name2. Array of Data
The Mongo IDThe 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 12Time Machine PID Number
1. A timestamp 2. The machine the id was created on
3. The process id 4. A random or auto-incrementing number
Batch InsertsBatch inserts are for entering multiple rows of data. Batch inserts are faster than insert a single dataset at a time when multiple datasets are present.
Insert data at different indexes in an array
www.prodigyview.com
Executing A Batch InsertA batch insert will return multiple id’s in array of the new documents that were created
3. Set the option for batchInsert to true
1. The collection name 2. Multiple arrays to insert
4. The array of ids for the new documents
www.prodigyview.com
Select QueriesSelect queries in Mongo are passed through as arrays also. The queries use the key => value and will return matching results.
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
ConditionalsSelect queries can also have conditionals in the array. These conditionals make for more complex searches.
For more information on Mongo’s Advanced Queries, visit here: http://www.mongodb.org/display/DOCS/Advanced+Queries
The $or conditional
Single DocumentWe can narrow down the search by only returning a single document from the database. The result differs because this will not be array of documents but a single document in an array.
1. Use the same select statement 2. Set findOne to true
3. Returns a single document in the format of an array
Updating FieldsUpdating fields use the same syntax as inserting and searching: your data is in an array. We are going to start by creating a new information to put in the document and search arguments.
1. Set the documents to have this value
2. Search where key is equal to these values
Update the FieldsAnd to perform the update, we pass our data into PVDatabase::updateStatement() .
1. Collection Name 2. Fields to update
3. Where to update the fields
www.prodigyview.com
WARNING
Remember when doing a regular update, add the old values with the new values, unless conditionals are used.
When you update a document, the ENTIRE DOCUMENT WILL BE REPLACED WITH THE UDPATE VALUES.
1. Entire Document will be replaced with this
Update With Conditionals
For more information on visit: http://www.mongodb.org/display/DOCS/Updating
A conditional operator
To prevent replacing the entire document, we can update Mongo DB with conditionals. These conditionals will only change the fields that are specified.
DeletingThe final action in this tutorial is going to be deleting a document from the database. We are going to use the same syntax as in our select statement for finding and delete the matching documents.
www.prodigyview.com
API ReferenceFor a better understanding of the database, visit the api by clicking on the link below.PVDatabase
www.prodigyview.com
More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials