> ## Content Index
> Fetch the complete content index at: https://scriptcrunch.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# MongoDB and Php Beginner Tutorials
- URL: https://scriptcrunch.com/mongodb-and-php-beginner-tutorials/
- Published: 2013-04-24T07:53:00.000Z
- Updated: 2026-06-23T08:56:26.000Z
- Author: Scriptcrunch Editorial
- Tags: #Migrated-1758801465347, #wp, #wp-post, #Import 2025-09-25 11:57

[Database](https://scriptcrunch.com/tag/databases/) connection:   
Connecting [mongoDB](https://scriptcrunch.com/tag/mongodb/) with php is really simple if your configured php with mongoDB using php driver. Assuming that you have already configured php with mongoDB am going to explain the php script to connect mongoDB. For configuring mongoDb with php inlinux , you can follow this link. ***[How to Configure Php with mongoDb on Ubuntu](http://%3C/?php%20//connect%20to%20mongoDB%20using%20mongo%20class%20$m%20=%20new%20Mongo%28%29;%20//selecting%20the%20database,%20which%20will%20be%20created%20on%20the%20fly%20$db%20=%20$m-%3EselectDB%28%22comtechies%22%29;%20//creating%20or%20selecting%20a%20collection.%20If%20the%20collection%20is%20already%20there%20it%20will%20be%20selected%20or%20it%20will%20be%20created%20on%20the%20fly.%20$collection%20=%20$db-%3Ecomtechies-%3Edemo;%20//creating%20a%20document%20with%20array%20$doc%20=%20array%28%20%20%20%20%20%20%20%20%20%22name%22%20=%3E%20%22bibin%22,%20%20%20%20%20%20%20%20%20%22place%22%20=%3E%20%22kollemcode%22,%20%20%20%20%20%20%20%20%20%22position%22%20=%3E%20%22admin%22%20%29;%20//inserting%20the%20document%20in%20to%20collection%20$collection-%3Einsert%28$doc%29;%20?%3E)***  
Sample Php web app using mongoDB:  
  
<?php  
  
//connect to mongoDB using mongo class  
$m = new Mongo();  
  
//selecting the database, which will be created on the fly  
$db = $m->selectDB("comtechies");  
  
//creating or selecting a collection. If the collection is already there it will be selected or it will be created on the fly.  
  
$collection = $db->comtechies->demo;  
  
//creating a document with array  
$doc = array(  
        "name" => "bibin",  
        "place" => "kollemcode",  
        "position" => "admin"  
);  
  
//inserting the document in to collection  
$collection->insert($doc);  
  
//store the first row in the collection to the variable result  
$result = $collection->findone();  
  
//shows the result  
var_dump($result);  
  
  
?>