SlideShare a Scribd company logo
1 of 18
PHP&MongoDB intro
• Instructiunile MongoDB urmatoare se pot scrie in:
1. shell-ul MongoDB(se porneste din C:/Program
Files/MongoDB/Server/3.2/bin/mongo.exe
1. Fereastra IntelliShell a Studio3T
1. NetBeans
In shell-ul Mongodb vom scrie:
>show dbs
pentru a vedea bazele de date de pe server
Observam ca pe server exista numai baza de date local. Pentru a lucra
cu baza de date elevi, si chiar pentru a o crea (daca aceasta nu exista)
vom scrie:
>use elevi
Pentru a vedea baza de date selectata in prezent:
>db
Daca vom scrie
>show dbs
Vom observa ca baza de date creata nu apare in lista! Aceasta
deoarece nu are nici o colectie.
Pentru a vedea colectiile(tabelele) din aceasta baza de date vom scrie:
>show collections
• Pentru a crea o noua baza de date, in IntelliShell vom selecta Create
database:
• Apoi vom specifica numele elevi:
• Pentru a adauga colectia(tabelul): Negruzzi in aceasta baza de date,
vom selecta Add Collection
• Pentru a adauga inregistrari in colectia Negruzzi, selectam „+“:
• si apoi:
• Repetati pasii pentru a mai insera cateva documente:
• Aceleasi lucruri se pot obtine si scriind comenzi in shell-ul Mongodb
• Pentru selecta toate documentele din colectia Negruzzi putem scrie
in IntelliShell/MongoChef: (SELECT * FROM Negruzzi)
• Pentru selecta documentele din colectia Negruzzi care au numele
„Popescu“ putem scrie in IntelliShell: (SELECT * FROM Negruzzi
WHERE nume=„Popescu“)
• Pentru ordona documentele din colectia Negruzzi dupa nume
ascendent putem scrie in IntelliShell: (SELECT * FROM Negruzzi
ORDER BY nume)
• Pentru ordona documentele din colectia Negruzzi dupa nume
descendent putem scrie in IntelliShell: (SELECT * FROM Negruzzi
ORDER BY nume DESC)
PHP&MangoDB- create a collection
connection.php
<?php
// connect to mongodb
$client = new MongoDBDriverManager("mongodb://localhost:27017");?>
PHP&MongoDB- insert a document
<?php
require_once 'connection.php';
$bulk = new MongoDBDriverBulkWrite;
$data=array(
'_id' => new MongoDBBSONObjectID,
'nume'=>"Popescu",
'age'=>"12",
'prenume'=>"Bogdan",
'movie'=>"Star Wars",
'marime'=>"" ,
'pret'=>"",
);
$bulk->insert($data);
$client->executeBulkWrite('elevi.Negruzzi',$bulk);
echo "Document inserted successfully";
?>
PHP&MongoDB- find all documents
<?php
require_once 'connection.php';
$query = new MongoDBDriverQuery([]);
$rows = $client->executeQuery("elevi.Negruzzi", $query);
foreach ($rows as $doc) {
echo $doc->nume . “<br/>";
}
?>
PHP&MongoDB- update a document
<?php
require_once 'connection.php';
$bulk = new MongoDBDriverBulkWrite;
$data=[
'nume'=>"Georgescu",
];
$filter=['nume'=>"Popescu"];
$update=['$set'=>$data];
$bulk->update($filter, $update);
$client->executeBulkWrite('elevi.Negruzzi',$bulk);
header('location:index.php');
?>
PHP&MongoDB- delete a document
<?php
require_once 'connection.php';
$bulk = new MongoDBDriverBulkWrite;
$filter = ['nume' => "Georgescu"];
$bulk->delete($filter);
$client->executeBulkWrite('elevi.Negruzzi',$bulk);
header('location:index.php');
?>

More Related Content

More from Razvan Raducanu, PhD (20)

10. view one record
10. view one record10. view one record
10. view one record
 
9. add new record
9. add new record9. add new record
9. add new record
 
8. vederea inregistrarilor
8. vederea inregistrarilor8. vederea inregistrarilor
8. vederea inregistrarilor
 
7. copy1
7. copy17. copy1
7. copy1
 
6. hello popescu 2
6. hello popescu 26. hello popescu 2
6. hello popescu 2
 
5. hello popescu
5. hello popescu5. hello popescu
5. hello popescu
 
4. forme in zend framework 3
4. forme in zend framework 34. forme in zend framework 3
4. forme in zend framework 3
 
3. trimiterea datelor la vederi
3. trimiterea datelor la vederi3. trimiterea datelor la vederi
3. trimiterea datelor la vederi
 
2.routing in zend framework 3
2.routing in zend framework 32.routing in zend framework 3
2.routing in zend framework 3
 
1. zend framework intro
1. zend framework intro1. zend framework intro
1. zend framework intro
 
18. images in symfony 4
18. images in symfony 418. images in symfony 4
18. images in symfony 4
 
17. delete data
17. delete data17. delete data
17. delete data
 
16. edit data
16. edit data16. edit data
16. edit data
 
15. view single data
15. view single data15. view single data
15. view single data
 
14. add data in symfony4
14. add data in symfony4 14. add data in symfony4
14. add data in symfony4
 
13. view data
13. view data13. view data
13. view data
 
12.doctrine view data
12.doctrine view data12.doctrine view data
12.doctrine view data
 
11. move in Symfony 4
11. move in Symfony 411. move in Symfony 4
11. move in Symfony 4
 
10. add in Symfony 4
10. add in Symfony 410. add in Symfony 4
10. add in Symfony 4
 
9. lower in Symfony 4
9. lower in Symfony 49. lower in Symfony 4
9. lower in Symfony 4
 

3. Php MongoDB intro

  • 2. • Instructiunile MongoDB urmatoare se pot scrie in: 1. shell-ul MongoDB(se porneste din C:/Program Files/MongoDB/Server/3.2/bin/mongo.exe 1. Fereastra IntelliShell a Studio3T 1. NetBeans
  • 3. In shell-ul Mongodb vom scrie: >show dbs pentru a vedea bazele de date de pe server Observam ca pe server exista numai baza de date local. Pentru a lucra cu baza de date elevi, si chiar pentru a o crea (daca aceasta nu exista) vom scrie: >use elevi Pentru a vedea baza de date selectata in prezent: >db Daca vom scrie >show dbs Vom observa ca baza de date creata nu apare in lista! Aceasta deoarece nu are nici o colectie.
  • 4. Pentru a vedea colectiile(tabelele) din aceasta baza de date vom scrie: >show collections • Pentru a crea o noua baza de date, in IntelliShell vom selecta Create database:
  • 5. • Apoi vom specifica numele elevi:
  • 6. • Pentru a adauga colectia(tabelul): Negruzzi in aceasta baza de date, vom selecta Add Collection
  • 7.
  • 8. • Pentru a adauga inregistrari in colectia Negruzzi, selectam „+“: • si apoi:
  • 9. • Repetati pasii pentru a mai insera cateva documente: • Aceleasi lucruri se pot obtine si scriind comenzi in shell-ul Mongodb
  • 10. • Pentru selecta toate documentele din colectia Negruzzi putem scrie in IntelliShell/MongoChef: (SELECT * FROM Negruzzi)
  • 11. • Pentru selecta documentele din colectia Negruzzi care au numele „Popescu“ putem scrie in IntelliShell: (SELECT * FROM Negruzzi WHERE nume=„Popescu“)
  • 12. • Pentru ordona documentele din colectia Negruzzi dupa nume ascendent putem scrie in IntelliShell: (SELECT * FROM Negruzzi ORDER BY nume)
  • 13. • Pentru ordona documentele din colectia Negruzzi dupa nume descendent putem scrie in IntelliShell: (SELECT * FROM Negruzzi ORDER BY nume DESC)
  • 14. PHP&MangoDB- create a collection connection.php <?php // connect to mongodb $client = new MongoDBDriverManager("mongodb://localhost:27017");?>
  • 15. PHP&MongoDB- insert a document <?php require_once 'connection.php'; $bulk = new MongoDBDriverBulkWrite; $data=array( '_id' => new MongoDBBSONObjectID, 'nume'=>"Popescu", 'age'=>"12", 'prenume'=>"Bogdan", 'movie'=>"Star Wars", 'marime'=>"" , 'pret'=>"", ); $bulk->insert($data); $client->executeBulkWrite('elevi.Negruzzi',$bulk); echo "Document inserted successfully"; ?>
  • 16. PHP&MongoDB- find all documents <?php require_once 'connection.php'; $query = new MongoDBDriverQuery([]); $rows = $client->executeQuery("elevi.Negruzzi", $query); foreach ($rows as $doc) { echo $doc->nume . “<br/>"; } ?>
  • 17. PHP&MongoDB- update a document <?php require_once 'connection.php'; $bulk = new MongoDBDriverBulkWrite; $data=[ 'nume'=>"Georgescu", ]; $filter=['nume'=>"Popescu"]; $update=['$set'=>$data]; $bulk->update($filter, $update); $client->executeBulkWrite('elevi.Negruzzi',$bulk); header('location:index.php'); ?>
  • 18. PHP&MongoDB- delete a document <?php require_once 'connection.php'; $bulk = new MongoDBDriverBulkWrite; $filter = ['nume' => "Georgescu"]; $bulk->delete($filter); $client->executeBulkWrite('elevi.Negruzzi',$bulk); header('location:index.php'); ?>