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();

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.