NoSql Lab4
NoSql Lab4
Manufacturing
Jabalpur-482005, M.P., India
Mongodb Continuation.....
db.student.insertOne({name:"Std1",
roll_number:01})
db.student.insertOne({name:"Std2", dob:ISODate("2000-03-02"),
marks:[10,20,30]})
• Using this tool you are allowed to import the files like
JSON, CSV, or TSV in the MongoDB database.
Steps to Import data
• https://www.mongodb.com/try/download/database-tools ( Go to this link and download the zip
format of Mongodb Command Line Database Tools )
• Copy all the files present in the bin folder of the extracted folder to the Mongodb bin folder in
C:\Program Files\
• mongoimport --db database_name --collection collection_name --type csv --fields name,emp_id --file
"C:\Users\Janvi\Desktop\employee.csv"
Export data
• db.getUsers()
• atlasAdmin • clusterMonitor
• readWriteAnyDatabas • hostManager
e • backup
• readAnyDatabase • restore
• read • userAdminAnyDatabase
• readWrite • dbAdminAnyDatabase
• dbAdmin • root
• dbOwner • __system
• userAdmin
• clusterAdmin
• clusterManager
https://www.mongodb.com/docs/manual/reference/built-in-roles/
MongoDB - Replication
Replication is the process of synchronizing data across multiple servers.
Purpose:
• Redundancy & increased data
availability.
Benefits:
• Protection against server loss.
• Enhanced redundancy.
• Improved data availability
• Mitigation of hardware failure.
• Overcoming service
interruptions.
.
MongoDB - Replication
Replica
Set
MongoDB - Sharding
Sharding means spreading data across many machines. It's how MongoDB
copes with more and more data.
Why Sharding?
• In replication, all writes go to master node
• Latency sensitive queries still go to master
• Single replica set has limitation of 12 nodes
• Memory can't be large enough when active dataset is big
• Local disk is not big enough
• Vertical scaling is too expensive
MongoDB - Sharding
MongoDB - Php
Step 1 : Search for Pecl Php Mongodb
MongoDB - Php
Step 2 : Go to the latest DLL file
MongoDB - Php
Step 3 : Download the Thread Safe(TS) file
MongoDB - Php
Step 4 : Unzip the downloded folder and copy the dll file
MongoDB - Php
Step 5 : Go to ext folder inside the php folder in xammp and paste the dll file
MongoDB - Php
Step 6 : extention=php_mongodb.dll
MongoDB - Php
Step 7 : Download and run Composer-Setup.exe file
MongoDB - Php
Step 8 : Make sure that the path is correct
MongoDB - Php
Step 9 : run composer require "mongodb/mongodb=^1.0.0" on terminal inside
your folder in htdocs
Step 10: Now Restart your xampp and you can start creating your files inside
the htdocs folder in xammp
MongoDB - Php
Creating connection:
Conn.php : // select a database
<?php $db = $conn->mydb;
require 'vendor/autoload.php';
echo "Database mydb
// connect to mongodb
selected";
$conn= new MongoDB\Client("mongodb://localhost:27017");
echo "Connection to database successfully";
?>
MongoDB - Php
Inserting a document:
insert.php
<?php
require "vendor/autoload.php";
// Creating Connection
$conn = new MongoDB\Client("mongodb://localhost:27017");
// Creating Database
$db = $conn -> studentdb;
//Creating Collection
$collection = $db -> student_info;
// Insering Record
$collection->insertOne( [ 'name' =>'Janvi', 'roll_number' => 658] );
// Fetching Record
$record = $collection->find( [ 'name' =>'Janvi'] );
foreach ($record as $Student) {
echo $Student['name'], ': ', $Student['roll_number']."<br>";
MongoDB - Php
Query document:
find.php
<?php
require "vendor/autoload.php";
$conn = new
MongoDB\Client("mongodb://localhost:27017");
$db = $conn -> studentdb;
$collection = $db -> student_info;
?>