2. Overview
Objective
Learn the basics about the structure of MongoDB and how to
manipulate 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 ProdigyView
Estimated Time
15 Minutes
3. 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
4. Start With Collections
MongoDB uses collections for storing groups of data. A
collection can be thought as a table in a relational
database.
Collection Table
Collections hold what are known as documents.
Collection with Documents
5. Documents Attributes
Documents exist inside a collection. Documentation can
almost be thought of as rows in a sql database, except
they 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. 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
7. Mongo Connection
To 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
8. Set the Connection
Then connection to the database just like a normal
connection.
www.prodigyview.com
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. Insert The Data
On 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.
2. Array of Data
1. Collection Name
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 Number
1. A timestamp 2. The machine the id 3. The process id 4. A random or auto-
was created on incrementing number
12. Batch Inserts
Batch 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
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 name
4. The array of ids for the new documents 3. Set the option for batchInsert
to true
www.prodigyview.com
14. Select Queries
Select 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
15. Conditionals
Select queries can also have conditionals in the array. These
conditionals make for more complex searches.
The $or conditional
For more information on Mongo’s Advanced Queries, visit here:
http://www.mongodb.org/display/DOCS/Advanced+Queries
16. Single Document
We 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
17. Updating Fields
Updating 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
18. Update the Fields
And 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
19. WARNING
When you update a document, the ENTIRE DOCUMENT
WILL BE REPLACED WITH THE UDPATE VALUES.
1. Entire Document will be replaced with this
Remember when doing a regular update, add the old values with
the new values, unless conditionals are used.
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 operator
For more information on visit:
http://www.mongodb.org/display/DOCS/Updating
21. Deleting
The 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
22. API Reference
For a better understanding of the database, visit the api
by clicking on the link below.
PVDatabase
More Tutorials
For more tutorials, please visit:
http://www.prodigyview.com/tutorials
www.prodigyview.com