PHP and MongoDB Web Development Beginner’s Guide | Dynamic Web 2.0 Applications

PHP Programming Book | Learn PHP Language |
Click Here To Download Book
Pages 292
Views 1149
Size 4.5 MB
Download 156
Table Of Contents

Chapter 1: Getting Started with MongoDB
The NoSQL movement
Types of NoSQL databases
MongoDB – A document-based NoSQL database
Why MongoDB?
Who is using MongoDB?
MongoDB concepts—Databases, collections, and documents
Anatomy of document
BSON—The data exchange format for MongoDB
Similarity with relational databases
Downloading, installing, and running MongoDB
System requirements
Time for action – downloading and running MongoDB on Windows
Installing the 64-bit version
Time for action – downloading and running MongoDB on Linux
Installing MongoDB on OS X
Configuring MongoDB
Command-line parameters
File-based configuration
Stopping MongoDB
Hitting Control + C
From the mongo shell
Sending INT or TERM signal in UNIX
Creating databases, collections, and documents
Time for Action – creating databases, collections, and documents
Installing the PHP driver for MongoDB
Time for Action – installing PHP driver for MongoDB on Windows
Installing the PHP-MongoDB driver on Unix
Connecting to the MongoDB server from PHP
Creating a PHP-Mongo connection
Time for action – creating a connection to the MongoDB server from PHP
Configuring the PHP-MongoDB connection
Specifying timeout for the connection attempt
Chapter 2: Building your First MongoDB Powered Web App
A MongoDB powered blog
Have the MongoDB server running
Inserting documents in MongoDB
Time for action – building the Blog Post Creator
Creating databases and collections implicitly
Performing ‘safe’ inserts
Benefits of safe inserts
Specifying a timeout on insert
Setting the user generated _id
The MongoDate object
Querying documents in a collection
Time for action – retrieving articles from a database
The Mongo Query Language
The MongoCursor object
Conditional Queries
Doing advanced queries in MongoDB
Time for action – building the Blog Dashboard
Returning a subset of fields
Sorting the query results
Using count, skip, and limit
Performing range queries on dates
Updating documents in MongoDB
Time for action – building the Blog Editor
Optional arguments to the update method
Performing ‘upsert’
Using update versus using save
Using modifier operations
Setting with $set
Incrementing with $inc
Deleting fields with $unset
Renaming fields with $rename
Deleting documents in MongoDB
Time for action – deleting blog posts
Optional arguments to remove
Managing relationships between documents
Embedded documents
Referenced documents
Time for action – posting comments to blog posts
Embedded versus referenced – Which one to use?
Querying embedded objects
Chapter 3: Building a Session Manager
Understanding HTTP sessions
Understanding PHP native session handling
Time for action – testing native PHP session handling
Limitations of native PHP session handling
Implementing session handling with MongoDB
Extending session handling with session_set_save_handler
The SessionManager class
Time for action – building the SessionManager class
How the SessionManager works
The constructor
The open and close methods
The read method
The write method
The destroy method
The gc method
Putting the SessionManager in action
Time for action – putting SessionManager into action
Building the user authentication module
Time for action – building the User class
Creating the login, logout, and user profile page
Time for action – creating the login, logout, and profile page
Using good session practices
Setting low expiry times of session cookies
Using session timeouts
Setting proper domains for session cookies
Checking for browser consistency
Chapter 4: Aggregation Queries
Generating sample data
Time for action – generating sample data
Understanding MapReduce
Visualizing MapReduce
Performing MapReduce in MongoDB
Time for action – counting the number of articles for each author
Defining the Map function
Defining the Reduce function
Applying the Map and Reduce
Viewing the results
Performing MapReduce on a subset of the collection
Performing MongoDB MapReduce within PHP
Time for action – creating a tag cloud
Performing aggregation using group()
Time for action – calculating the average rating per author
Grouping by custom keys
MapReduce versus group()
Listing distinct values for a field
Time for action – listing distinct categories of articles
Using distinct() in mongo shell
Counting documents with count()
Chapter 5: Web Analytics using MongoDB
Why MongoDB is a good choice as a web analytics backend
Logging with MongoDB
Time for action – logging page visits with MongoDB
Capped collections
Sorting in natural order
Updating and deleting documents in a capped collection
Specifying the size of a regular collection
Convert a regular collection to a capped one
Extracting analytics data with MapReduce
Time for action – finding total views and average response time per blog post
The map, reduce, and finalize functions
Displaying the result
Running MapReduce in real time versus running it in the background
Real-time analytics using MongoDB
Time for action – building a real-time page visit counter
Chapter 6: Using MongoDB with Relational Databases
The motivation behind using MongoDB and an RDBMS together
Potential use cases
Defining the relational model
Time for action – creating the database in MySQL
Caching aggregation results in MongoDB
Time for action – storing the daily sales history of products in MongoDB
Benefits of caching queries in MongoDB
Storing results of expensive JOINs
Using MongoDB for data archiving
Time for action – archiving old sales records in MongoDB
Challenges in archiving and migration
Dealing with foreign key constraints
Preserving data types
Storing metadata in MongoDB
Time for action – using MongoDB to store customer metadata
Problems with using MongoDB and RDBMS together
Chapter 7: Handling Large Files with GridFS
What is GridFS?
The rationale of GridFS
The specification
Advantages over the filesystem
Storing files in GridFS
Time for action – uploading images to GridFS
Looking under the hood
Serving files from GridFS
Time for action – serving images from GridFS
Updating metdata of a file
Deleting files
Reading files in chunks
Time for action – reading images in chunks
When should you not use GridFS
Chapter 8: Building Location-aware Web Applications wi
MongoDB and PHP
A geolocation primer
Methods to determine location
Detecting the location of a web page visitor
The W3C Geolocation API
Browsers that support geolocation
Time for action – detecting location with W3C API
The Geolocation object
The getCurrentPosition() method
Drawing the map using the Google Maps API
Geospatial indexing
Time for action – creating geospatial indexes
Geospatial indexing – Important things to know
Performing location queries
Time for action – finding restaurants near your location
The geoNear() command
Bounded queries
Geospatial haystack indexing
Time for action – finding nearby restaurants that serve burgers
Chapter 9: Improving Security and Performance
Enhancing query performance using indexes
Time for action – creating an index on a MongoDB collection
The _id index
Unique indexes
Compound keys indexes
Indexing embedded document fields
Indexing array fields
Deleting indexes
When indexing cannot be used
Indexing guidelines
Choose the keys wisely
Keep an eye on the index size
Avoid using low-selectivity single key indexes
Be aware of indexing costs
On a live database, run indexing in the background
Optimizing queries
Explaining queries using explain()
Optimization rules
Using hint()
Profiling queries
Understanding the output
Optimization rules
Securing MongoDB
Time for action – adding user authentication in MongoDB
Creating an admin user
Creating regular user
Viewing, changing, and deleting user accounts
User authentication through PHP driver
Filtering user input
Running MongoDB server in a secure environment
Ensuring data durability
Using fsync
Chapter 10: Easy MongoDB Administration with RockMon
and phpMoAdmin
Administering MongoDB with RockMongo
Time for action – installing RockMongo on your computer
Exploring data with RockMongo
Updating, deleting, and creating documents
Importing and exporting data
Viewing stats
Using phpMoAdmin to administer MongoDB
Time for action – installing phpMoAdmin on your computer
Viewing databases and collections
Querying documents
Saving and deleting objects
Importing and exporting data
Viewing stats
Other features
RockMongo versus phpMoAdmin
The verdict
Pop Quiz Answers
Chapter 1, Getting Started with MongoDB
Chapter 2, Building your First MongoDB Powered Web App
Chapter 3, Building a Session Manager
Chapter 4, Aggregation Queries
Chapter 5, Web Analytics using MongoDB
Chapter 7, Handling Large Files with GridFS
Chapter 8, Building Location-aware Web Applications with MongoDB and PHP
Chapter 9, Improving Security and Performance