0% found this document useful (0 votes)
23 views3 pages

Handout 6-Indexing-Nosql: DB - Empinfo.Createindex ( ("Emp - Id": 1) )

The document provides examples of PHP code to perform various operations on MongoDB collections and documents. These include inserting documents, creating and querying indexes, updating and deleting documents, and using bulk operations. For example, it shows how to insert 3 documents into an employee collection using insertMany(), create an index on the "zip" field, update documents matching a query, and perform multiple operations in a single bulk write.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Handout 6-Indexing-Nosql: DB - Empinfo.Createindex ( ("Emp - Id": 1) )

The document provides examples of PHP code to perform various operations on MongoDB collections and documents. These include inserting documents, creating and querying indexes, updating and deleting documents, and using bulk operations. For example, it shows how to insert 3 documents into an employee collection using insertMany(), create an index on the "zip" field, update documents matching a query, and perform multiple operations in a single bulk write.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Handout 6-Indexing-Nosql

Q1. Insert the following documents into employee collection

{ "_id" : ObjectId("548eb3a051ca033c1c01054d"), "emp_id" : "SALS",


"emp_name" : "Pat", "designation" : "clerk", "department" : "sales" }
{ "_id" : ObjectId("548eb3a051ca033c1c01054e"), "emp_id" : "OFFC",
"emp_name" : "Jons", "designation" : "peon", "department" : "sales" }
{ "_id" : ObjectId("548eb3a051ca033c1c01054f"), "emp_id" : "CLC",
"emp_name" : "Tnit", "designation" : "president", "department" : "sales" }
Q2. Create the index on emp_id column.

> db.empinfo.createIndex( { "emp_id" : 1 } );

Q3. Insert the following documents into em_employee collection

{ "_id" : ObjectId("548fe6d898fd7de25e8d3aa9"),
"emp_id" : "SALS", "emp_name" : "Pat",
"address" : [ { "street" : "Court Street",
"city" : "London",
"zip" : "5698722" } ] }
{ "_id" : ObjectId("548fe6d898fd7de25e8d3aaa"),
"emp_id" : "OFFC", "emp_name" : "Jones",
"address" : [ { "street" : "Church Avenue",
"city" : "Paris",
"zip" : "632902" } ] }
Q4. create the index on zip field(embedded field)

> db.empinfo.ensureIndex( { "address.zip": 1 } );

Q5. Write a php program to display the documents of employee collection.

<?php
$mng = new MongoDB\Driver\Manager(“mongodb://localhost:27017”);
$query = new MongoDB\Driver\Query([]);
$rows = $mng->executeQuery(“db.employee”, $query);
foreach($rows as $row)
{echo “$row->name, $row->designation, $row->department\n”;} ?>

Q6. Write a php program to insert the documents of employee collection.

<?php
$mng = new MongoDB\Driver\Manager(“mongodb://localhost:27017”);
$bulkWrite = new MongoDB\Driver\BulkWrite;
$doc =[“_id”=>new MongoDB\BSON\ObjectID, 'emp_id' => 'FMP', ‘emp_name’
=> ‘John’, ’designation’=>’peon’, ‘department’ => 'Sales'];
$bulkWrite->insert($doc);
$manager->executeBulkWrite('db.employee', $bulkWrite);
echo “Inserted successfully\n”;
$query = new MongoDB\Driver\Query([]);
$rows = $mng->executeQuery(“db.employee”, $query);
foreach($rows as $row)
{echo “$row->emp_name,$row->designation,$row->department\n”;}
?>

Q7. Write a php program to update the document in employee collection to new values
values of department=”HR’ and designation=’Manager’ where emp_name=’Smith’

<? $m=new MongoDB\Driver\Manager(“mongodb://localhost:27017”);


$bulk = new MongoDB\Driver\BulkWrite;
$bulk.update( [‘name’=>’Smith’],[‘$set’ =>[‘dept’=>’ece’, ’inst’=>’rit’]],
[‘multi’=>true]);
$mng -> executeBulkWrite(‘db.employee’, $bulk);
echo “successfully updated”;
$query =new MongoDB\Driver\Query([]);
$rowz=$mng->executeQuery(“sample.scol”,$query);
foreach ($rows as $row)
{ echo “$row->emp_name, $row->designation, $row->department <br>”;} ?>
Q8. Write a php program to delete the document in employee collection whose name is
‘john’

<? $m=new MongoDB\Driver\Manager(“mongodb://localhost:27017”);


$bulk = new MongoDB\Driver\BulkWrite;
$bulk.delete([‘name’ => ‘john’],[justOne]);
$mng -> executeBulkWrite(‘db.employee’, $bulk);
echo “successfully updated”;
$query =new MongoDB\Driver\Query([]);
$rowz=$mng->executeQuery(“sample.scol”,$query);
foreach ($rows as $row)
{ echo “$row->emp_name, $row->designation, $row->department <br>”;}
?>
Q9. Write a php program to insert 3 documents in employee collection using insertMany().

db.employee.insert([
{ _id: 20, emp_name: "John Wick", emp_id: "se”, designation:’Tester’,
department: ’it’},
{_id:21,emp_id:’STR’,emp_name:’Moti’, designation:’Developer’,
department:’it’},
{_id:22,emp_id:’SE’,emp_name:’Mahesh’,designation:’Team Lead’,
department:’it’}])

Q10. Write a php program to perform multiple operations on aemployee collection using
bulkWrite().

<?php
$manager = new MongoDB\Driver\Manager
("mongodb: // localhost : 27017");
$bulk = new MongoDB\Driver\BulkWrite([‘ordered’=>true]);
$bulk->insert(['_id' => 1, 'x' => 1]);
$bulk->insert(['_id' => 2, 'x' => 2]);
$bulk->update(['x' => 2], ['$set' => ['x' => 1]]);
$bulk->insert(['_id' => 3, 'x' => 3]);
$bulk->delete(['x' => 1]);
$manager->executeBulkWrite('db.collection', $bulk); ?>

You might also like