Database Reference
In-Depth Information
Chapter 6
PHP and MongoDB
Through the first five chapters, you've learned how to perform all sorts of actions in the MongoDB shell. For example,
you've learned how to add, modify, and delete a document. You've also learned about the workings of DBRef and GridFS,
including how to use them.
So far, however, most of the things you've learned about have taken place in the MongoDB shell. It is a very
capable application, but the MongoDB software also comes with a vast number of additional drivers (see Chapter 2
for more information on these) that let you step outside the shell to accomplish many other sorts of tasks
programmatically.
One such tool is the PHP driver, which allows you to extend your PHP installation to connect, modify, and
manage your MongoDB databases when you want to use PHP rather than the shell. This can be helpful when you
need to design a web application, or don't have access to the MongoDB shell. As this chapter will demonstrate, most
of the actions you can perform with the PHP driver closely resemble functions you can execute in the MongoDB
shell; however, the PHP driver requires that the options be specified in an array, rather than between two curly
brackets. Similarities notwithstanding, you will need to be aware of quite a few however s when working with the PHP
driver. This chapter will walk you through the benefits of using PHP with MongoDB, as well as how to overcome the
aforementioned “howevers.”
This chapter brings you back to the beginning in many ways. You will start by learning to navigate the database
and use collections in PHP. Next you will learn how to insert, modify, and delete posts in PHP. You will also learn how
to use GridFS and DBRef again; this time, however, the focus will be on how to use them in PHP, rather than the theory
behind these technologies.
Comparing Documents in MongoDB and PHP
As you've learned previously, a document in a MongoDB collection is stored using a JSON-like format that consists of
keys and values. This is similar to the way PHP defines an associative array, so it shouldn't be too difficult to get used
to this format.
For example, assume a document looks like the following in the MongoDB shell:
contact = ( {
"First Name" : "Philip",
"Last Name" : "Moran",
"Address" : [
{
"Street" : "681 Hinkle Lake Road",
"Place" : "Newton",
"Postal Code" : "MA 02160",
"Country" : "USA"
}
],
 
Search WWH ::




Custom Search