Last Updated: February 25, 2016
·
977
· peterpay

mongoDB easy db creation php

To create a db on runtime from php:

$conn = new Mongo('serverConnectionString');

create a db

$db = $conn->selectDB("dbName"); 

on the first command like adding a collection to that db will trigger the creation

$db->command(array("create" =>"newCollectionName")); 

also security sometime is needed so :

$db->selectCollection("system.users"); 
$db->insert(array('user' =>"username", 'password'=> md5('password'),'readOnly' => false));

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside