Reader small image

You're reading from  MongoDB 4 Quick Start Guide

Product typeBook
Published inSep 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789343533
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Doug Bierer
Doug Bierer
author image
Doug Bierer

Doug Bierer has been hooked on computers since his first program, written on a DEC PDP-8, in 1971. In his wide-ranging career, he has been a professional contract programmer since 1978, having written applications in BASIC, PL/I, assembler, FORTH, C, C++, dBase/FoxBase/Clipper, Pascal, Perl, Java, and PHP. He deployed his first website in 1993 while living in San Francisco. He speaks four languages, has traveled extensively, and now resides in Thailand. He also spent some years doing system administration and TCP/IP networking. Some of his technical works include PHP 7 Programming Cookbook and Learning MongoDB 4.x (Packt), as well as Learning PHP and MySQL, Learning PHP Security and Learning Doctrine (O'Reilly Media).
Read more about Doug Bierer

Right arrow

Developing with Program Language Drivers

In Chapter 3, Using the Mongo Shell, you learned how to formulate basic CRUD operations using the JavaScript functions which the shell executed. In this chapter, you will learn how the same sort of operations can be performed using the PHP MongoDB driver. In addition, this chapter summarizes external PHP libraries and the integration of the Mongo PHP driver with various PHP frameworks.

The reason why PHP was chosen is because it is one of the most widely used web programming languages and is easy to understand, especially if you have a background in C language. Other popular languages such as Java, JavaScript, and Python are already well-documented on the MongoDB website (https://docs.mongodb.com/ecosystem/drivers/) and have plenty of examples, whereas this is not the case for PHP.

The version of PHP featured in this chapter is PHP 7.2...

Overview of the MongoDB PHP driver

There are two primary PHP language drivers for MongoDB:

The original mongo extension was introduced in 2009. The latest version (1.6.16) was released in September 2017. It is now deprecated and its use is discouraged. The first stable version of the current extension was released in October 2016. At the time of writing this book, the most recent version was released 9 July 2018. For release information and direct access to the source code, go to http://pecl.php.net/package/mongodb.

MongoDB PHP architecture

The big difference between the old and...

Installing the MongoDB PHP extension

In this section, we will cover the steps to install the MongoDB PHP extension on Windows and Linux/Mac. It is highly recommended for both Linux and macOS users to use PECL (http://pecl.php.net/) to install ext/mongodb. PECL stands for PHP Extension Community Library. It is a repository which contains PHP extensions that are written in C language, which must be compiled and enabled in the php.ini file. For a Windows installation, on the other hand, all you need to do is to install the appropriate *.dll file, and enable the extension in the php.ini file.

ext/mongodb installation using PECL

The pecl command is used for low level PHP engine packaging and distribution. This system is shared...

Performing CRUD operations using PHPLIB

Something of critical interest to developers is how to perform basic CRUD operations using ext/mongodb. You will note that PHPLIB, by design, is very closely modeled after the MongoDB JavaScript command set covered in Chapter 3, Using the Mongo Shell. The main difference is that instead of having arguments formulated as JSON expressions, when using PHPLIB, expressions use native PHP data types (for example, arrays, objects, and so on).

Core PHPLIB class instances needed for CRUD operations

It is important to note that all of the CRUD operations that we are going to discuss require that you create a MongoDB\Collection instance. This can be accomplished in several ways, including:

  • MongoDB...

Other libraries and framework integration

There are a number of PHP packages which are based upon the MongoDB Driver for PHP (which is to say ext/mongodb and PHPLIB). These packages can be installed using Composer, and are listed on packagist.org. The following table summarizes these packages. If a package is not specifically designed for any particular framework, it is marked in the table as standalone. The packages are listed in order of the number of downloads, as recorded by the Packagist website. Packages with less than 200,000 downloads are not included:

Package Works With ... Notes
jenssegers/mongodb Laravel Mini-framework for building a library of commonly used model classes. Supports the Eloquent set of models with a query builder.

doctrine/mongodb

doctrine/mongodb-odm

Doctrine Provides Object Document Mapping capabilities to MongoDB via the Doctrine Project...

Summary

In this chapter, you learned how to access MongoDB from PHP. You learned that there are two extensions, ext/mongo, which has been deprecated, and ext/mongodb. The latter extension actually consists of three layers: low level libraries supplied by the OS, drivers for PHP 5, PHP 7, and HVVM, and a high level library called PHPLIB. You then learned that installation on Windows involves choosing the right *.dll file based on your PHP installation, whereas for Linux and Mac it is recommended to perform the installation using PECL.

You learned how to perform Create, Read, Update, and Delete operations using PHPLIB. You also learned that you cannot directly search the auto-generated object ID, but rather need to create an ObjectId instance first, and then use that in the search. Finally, you learned that there are many PHP libraries for MongoDB which are available on the Packagist...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
MongoDB 4 Quick Start Guide
Published in: Sep 2018Publisher: PacktISBN-13: 9781789343533
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Doug Bierer

Doug Bierer has been hooked on computers since his first program, written on a DEC PDP-8, in 1971. In his wide-ranging career, he has been a professional contract programmer since 1978, having written applications in BASIC, PL/I, assembler, FORTH, C, C++, dBase/FoxBase/Clipper, Pascal, Perl, Java, and PHP. He deployed his first website in 1993 while living in San Francisco. He speaks four languages, has traveled extensively, and now resides in Thailand. He also spent some years doing system administration and TCP/IP networking. Some of his technical works include PHP 7 Programming Cookbook and Learning MongoDB 4.x (Packt), as well as Learning PHP and MySQL, Learning PHP Security and Learning Doctrine (O'Reilly Media).
Read more about Doug Bierer