Database Reference
In-Depth Information
This example creates a connection to the database service. Next, you will learn how to disconnect from the
service. Assuming you used the method just described to connect to your database, you can call $connection again to
pass the close() command to terminate the connection, as in this example:
$connection->close();
The close doesn't need to be called, except in unusual circumstances. The reason for this is that the PHP driver
closes the connection to the database once the Mongo object goes out of scope. Nevertheless, it is recommended that
you call close() at the end of your PHP code; this helps you avoid keeping old connections from hanging around
until they eventually time out. It also helps you ensure that any existing connection is closed, thereby enabling a new
connection to happen, as in the following example:
$connection = new Mongo();
$connection->close();
$connection->connect();
The following snippet shows how this would look like in PHP:
<?php
// Establish the database connection
$connection = new Mongo()
// Close the database connection
$connection->close();
?>
Installing the Python Driver
Python is a general-purpose and easy-to-read programming language.
These qualities make Python a good language to start with when you are new to programming and scripting.
It's also a great language to look into if you are familiar with programming, and you're looking for a multi-paradigm
programming language that permits several styles of programming (object-oriented programming, structured
programming, and so on). In the upcoming sections, you'll learn how to install Python and enable MongoDB support
for the language.
Installing PyMongo under Linux
Python offers a specific package for MongoDB support called PyMongo. This package allows you to interact with the
MongoDB database, but you will need to get this driver up and running before you can use this powerful combination.
As when installing the PHP driver, there are two methods you can use to install PyMongo: an automated approach
that relies on setuptools or a manual approach where you download the source code for the project. The following
sections show you how to install PyMongo using both approaches.
Installing PyMongo Automatically
The pip application that comes bundled with the python-pip package lets you automatically download, build, install,
and manage Python packages. This is incredibly convenient, enabling you to extend your Python modules installation
even as it does all the work for you.
 
Search WWH ::




Custom Search