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

Getting from a Web Form to MongoDB

In this chapter, we return once again to programming language drivers. You will learn what is involved in making a query using jQuery data tables for a PHP program which accesses a MongoDB database on the backend. You will then see how a new purchase is added using jQuery autocomplete fields, that is, by making AJAX queries via PHP to the MongoDB database, thus returning a JSON response. As part of the insert process, you will learn how to invoke transaction support*, embed customer and product documents into a purchase document, and add an entry to an embedded array. In addition, we will discuss adding x.509 certificates and authentication and access control.

The demo server used for this chapter is running Ubuntu Linux 18.04.

The topics to be covered in this chapter are as follows:

  • Building the application
  • Configuring transaction support
  • Adding...

Building the application

The first step in building the application is to make sure that the underlying programming driver infrastructure is in place. This has already been covered in detail in Chapter 4, Developing with Program Language Drivers. Here is a brief overview of the steps to take:

  1. Install PHP X.Y and the phpX.Y-dev library, where X.Y is the target version (for example, PHP 7.2)
  2. Install PECL by installing php-pear
  3. Install the PHP mongodb extension using pecl
  4. Install the PHP MongoDB Library using the composer require mongodb/mongodb command

Once the programming language driver infrastructure is in place, we can turn our attention to application development. The objective in this illustration is to provide a way to view and add purchases using the sweetscomplete database. To refresh your memory, the purchases document appears as follows:

We will first turn our attention...

Configuring transaction support

In addition to getting a MongoDB\Driver\Session instance and supplying it as an argument to the insert() and update() methods, we must also provide the appropriate configuration. In the demonstration application, json.php and add.php both pulled connection parameters from a file called Application/init.php. The basic configuration for a simple MongoDB database with no security is as follows:

In order to provide support for transactions, we must run the operation on a MongoDB replica set member. The revised configuration file, Application\init.php, would appear as follows:

Our Application\Connection::buildUri() method will then create the following MongoDB connection string:

mongodb://192.168.2.107:27017/sweetscomplete?replicaSet=sweets_11

We must also add to the MongoDB config file and ensure that the MongoDB instance is a member of the replica...

Adding security

As with transactions, when adding security, we need to modify the MongoDB connection string. Assuming that we are using x.509 certificates, and assuming that we have created a database user zed, here is the modified Application/init.php file:

And this is the connect string which is produced:

mongodb://zed:password@mongod/sweetscomplete?ssl=1&replicaSet=sweets_11&authSource=admin

The MongoDB conf file also needs to be modified, as described in Chapter 7, Securing MongoDB:


Summary

In this chapter, you learned how to configure a PHP-based application which performs jQuery lookups, queries the MongoDB database, and produces a JSON response. You also learned how to configure the application to provide support for transactions and security. Among the techniques demonstrated here, you learned how to embed documents and update an embedded array.

This chapter concludes this book. The purpose of this book is to present you with enough information to get a MongoDB installation up and running in a safe and secure manner. Along the way, we also showed you some of the advanced features that are unique to MongoDB such as the aggregation pipeline, replication, and sharding. It is our hope that you will find this book a useful addition to your reference library, and that it encourages you to explore other Packt books in this series.

...
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