Beginners Tutorial for MongoDB
💻 coding

Beginners Tutorial for MongoDB

2 min read 350 words
2 min read
ShareWhatsAppPost on X
  • 1MongoDB is a NoSQL database that stores data in structured JSON-like documents with dynamic schemas.
  • 2The tutorial guides users on creating a simple login page using PHP and MongoDB, including installation instructions.
  • 3Users can connect to MongoDB with or without authentication and create databases and collections easily.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"MongoDB is a NoSQL database that stores data in structured JSON-like documents with dynamic schemas."

Beginners Tutorial for MongoDB

Now time to work with MongoDB NoSQL database management systems, it stores data into structured JSON like documents with dynamic schemas and improves application performance. This tutorial helps you how to create a simple login page using PHP and MongoDB. Try this download script and implement a scalable application.

Installation

Windows

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

You can download PHP mongo drive/extension DLL file from following URL https://github.com/mongodb/mongo-php-driver/downloads. Copy php_mongo.dll file into your PHP extension directory

Linux & Mac

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Connecting into MongoDB

PHP code connecting without Authentication.

<?php
$mongo = new Mongo();
$db = $mongo->selectDB(“test”);
?>

With Authentication

<?php
$mongo = new Mongo(“mongodb://{$username}:{$password}@{$host}”);
$db = $mongo->selectDB(“test”);
?>

By default, MongoDB contains a sample database called "test".

Creating a New Database

$db = $mongo->Database_Name;

Note: Technically, you don't need to manually create databases in MongoDB due to its schemaless "lazy" way of creating databases.

Queries

PHP code to get MongoDB databases.

//Get list of databases
$mongo->admin->command(array(“listDatabases” => 1));
//Get list of Collections in test db
$db->listCollections();

Mongo Shell/Terminal/Command Prompt

db.listDatabases
db.test.showCollections

Create Collection(table)

You can create a collection/table but execute the following code.

$db->createCollection(“people”,false);

Note: here false referred to unlimited size, if you give true you have to mention the size of maximum.

Mongo Shell

db.createCollection(“people”,false);

Insert Record

Insert records int MongoDB.

<?php
$people = $db->people;
$insert = array(“user” => “demo@askgif.info”, “password” => md5(“demo_password”));
$db->insert($insert);
?>

Mongo Shell:

db.people.insert({user:”user_name”,password:”password”});

HTML Code

<form action="index.php" method="POST">
Email:
<input type="text" id="usr_email" name="usr_email" />
Password:
<input type="password" id="usr_password" name="usr_password" /> 
<input name="submitForm" id="submitForm" type="submit" value="Login" />
</form>

PHP Code

<?php
$succss = "";
if(isset($_POST) and $_POST['submitForm'] == "Login" )
{
$usr_email = mysql_escape_string($_POST['usr_email']);
$usr_password = mysql_escape_string($_POST['usr_password']);
$error = array();
// Email Validation
if(empty($usr_email) or !filter_var($usr_email,FILTER_SANITIZE_EMAIL))
{
$error[] = "Empty or invalid email address";
}
if(empty($usr_password)){
$error[] = "Enter your password"; 
}
if(count($error) == 0){
$con = new Mongo();
if($con){
// Select Database
$db = $con->test;
// Select Collection
$people = $db->people;
$qry = array("user" => $usr_email,"password" => md5($usr_password));
$result = $people->findOne($qry);
if($result){
$success = "You are successully loggedIn";
// Rest of code up to you.... 
} 
} else {
die("Mongo DB not installed");
} 
}
} 
?>

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 29 August 2019 · 2 min read · 350 words

Part of AskGif Blog · coding

You might also like

Beginners Tutorial for MongoDB | AskGif Blog