MongoDB

When using a document-orientated database like MongoDB, you let your application take control of the data structure instead of the database. This seems unconventional for developers used to working with relational databases (like me) but it can help to speed up the development process. 

Drop MongoDB database in PHP


// Establish connection.
$connection = new Mongo();

// Select database.
$database = $connection->demo;

// Drop database.
$dropped = $database->drop();
var_dump($dropped);

// Close connection.
$connection->close();

First steps using MongoDB in PHP


// Establish connection.
$connection = new Mongo();
// Select database, create database if database not exists.
$database = $connection->demo;
// Select collection.
$collection = $database->users;
// Insert data.
$user = array(
  'name' => 'Dribbit',
  'email' => 'info@dribbit.eu',
);
$collection->insert($user);

// Get all records.
$records = $collection->find();
var_dump($records);
foreach($records as $record) {
  var_dump($record);
}

// Close connection.
$connection->close();

Where does mongodb stores data

Type following command in command line terminal:

sudo nano /etc/mongodb.conf

 

mongo_conf.png

This is based on an Ubuntu system.

Install MongoDB driver for PHP on Ubuntu

Type following line in command line:

sudo pecl install mongo

If Pecl throws this error: configure: error: sasl.h not found! add following command and retry installing mongodb

sudo apt-get install libsasl2-dev

After installing, add the mongo extension to php.ini

extension=mongo.so

Restart apache:

Install MongoDB on Ubuntu

32 bit systems are limited to DB of 2 gig. Therefor, use MongoDB only on 64 bit systems in production.

@see MongoDb documentation to install MongoDB.

See if MongoDB is running by typing

sudo service mongodb status

in command line.

 

Subscribe to RSS - MongoDB