Tag archive for: NoSQL

Getting documents from MongoDB collections with PHP

We have a MongoDB server running as a windows service on the same machine as our web server. Over time, this MongoDB server had been listening on port 33333 to help us remember information that we had collected about our friends.

As time passes, our memory can hardly rival that of our MongoDB server, which was designed to help us recollect information efficiently. This post documents some proof of concept that I did for querying documents from MongoDB collection via the PHP driver.

Connecting to and disconnecting from a MongoDB server in PHP

The first step to manipulate data in the MongoDB ecosystem is to connect to a MongoDB server. In php, we can use the Mongo class to help us connect to one or more MongoDB servers.

For the purpose of this demonstration, let’s assume that we had installed a MongoDB server instance as a windows service which listens on port 12345. In addition, our PHP web server runs on the same machine as the MongoDB instance.

Installing MongoDB as a windows service

After using MongoDB for quite a while, I realised that I had been repeating the starting up of the MongoDB database daemon whenever I reboot my PC. Manually starting the MongoDB database daemon via my command prompt whenever I want to work on my projects is inefficient.

Hence making MongoDB run as a windows service automatically when my computer starts up is one way to avoid repeating myself. In this post, I document how I install my MongoDB database as a windows service.