NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

use dbname
show dbs

db.createCollection("student")
show collections
db.student.drop()


------------------------------
Practical List 1
use Mongo_demo
//Students(enro, fname, lname, city, gender, contactno, dob)

db.createCollection("Students")
show collections

db.Students.insertMany([
{"enro": 4,"Fname":"Isha","Lname":"Motiyani","City":"Surat","Gender":"F","contactno":9874563210,"dob":"12/09/2000"},
{"enro": 102,"Fname":"Anish","Lname":"Patel","City":"Valsad","Gender":"M","contactno":9632587410,"dob":"06/01/2001"},
{"enro": 103,"Fname":"Kalp","Lname":"Shah","City":"Baroda","Gender":"M","contactno":9630125874,"dob":"13/10/2000"},
{"enro": 104,"Fname":"Yash","Lname":"Bhakta","City":"Mumbai","Gender":"M","contactno":9987452103,"dob":"08/05/2001"},
{"enro": 84,"Fname":"Aparana","Lname":"Mukherjee","City":"Surat","Gender":"F","contactno":8563247890,"dob":"26/06/2001"},
{"enro": 8,"Fname":"Arjun","Lname":"Poduval","City":"Vapi","Gender":"M","contactno":8965230147,"dob":"01/05/2001"},
{"enro": 105,"Fname":"Riya","Lname":"Agrawal","City":"Navsari","Gender":"F","contactno":6985321470,"dob":"02/09/2000"},
{"enro": 79,"Fname":"Monis","Lname":"Khan","City":"Bardoli","Gender":"M","contactno":7895231053,"dob":"23/11/2000"},
{"enro": 106,"Fname":"Abhishek","Lname":"Choksi","City":"Surat","Gender":"M","contactno":6985327410,"dob":"18/07/2001"},
{"enro": 49,"Fname":"Om","Lname":"Patel","City":"Valsad","Gender":"M","contactno":9985632100,"dob":"15/05/2001"},
]);

1.
db.Students.find().pretty();

2.Show only the first name and last name of all the students.
db.Students.find({ }, { "Fname" : 1 ,"Lname" : 1 , _id:0 } );

3.Display the list of unique cities of the students.
db.Students.distinct("City");

4.Retrieve the student‟s date of birth along with the student first name and last name.
db.Students.find({ }, { "dob":1,"Fname" : 1 ,"Lname" : 1 , _id:0 } );

5.Get all the details of students whose city is “Surat”.
db.Students.find({ "City" : "Surat"},{}).pretty();

6.Arrange all the documents of students in ascending order using “enro” number.
db.Students.find().sort({"enro" : -1 }).pretty()

7.Show the details of just the first five documents of the students.
db.Students.find().limit(5)

8.Display only the details of last three documents of the students.
db.Students.find().skip(db.Students.count()-3)

9.View the list of male students whose city is “Valsad”.
db.Students.find({ "City" : "Valsad","Gender":"M"},{}).pretty();

10.Display all the documents in descending order.
db.Students.find().sort({"enro" : 1 }).pretty()

11.Display the list of first four students whose city is “Valsad” in order.
db.Students.find({"City":"Valsad"}).limit(4)

12.Update the contact number and date of birth of “Monis” to “6852987530” and “06/01/1999” respectively.
db.Students.update( { "Fname" : "Monis" },{ $set: {"contactno" : 6852987530 , "dob" : "06/01/1999"} } );

13.Find only the date of birth of “Om”.
db.Students.find({"Fname":"Om"},{"Fname":1,"dob":1,_id:0});

14.Insert the document (100, Rohit, Mehera, Mumbai, 24/03/2003) in the collection.
db.Students.insertOne({"enro": 100,"Fname":"Rohit","Lname":"Mehera","City":"Mumbai","dob":"24/03/2003"})

15.Display the list of male students whose last name is “Patel”.
db.Students.find({"Lname":"Patel","Gender":"M"},{_id:0});

16.Insert the document (35, Priyank, Mistry, Anand, 18/06/2000, 9000800001) in the collection.
db.Students.insertOne({"enro": 35,"Fname":"Priyank","Lname":"Mistry","City":"Anand","dob":"18/06/2000","contactno":9000800001})

17.Show all the details of student whose enrollment number is “102”.
db.Students.find({"enro":102},{}).pretty();

18.Display the list of students whose gender in “NULL” or 0.
db.Students.find({$or:[{"Gender":"NULL"},{"Gender":0}]}).pretty();

19.Complete all details of students, if any document of student the document is incomplete.
db.Students.update( { Fname : "Rohit" },{ $set: {"Gender":"M","contactno":8200950913 } } );
db.Students.update( { Fname : "Priyank" },{ $set: {"Gender":"M" } } );

20.Remove all the details of students whose city is “Vapi”.
db.Students.remove({"City":"Vapi"})

21.Display the next five students after skipping first five which are in the collection.
db.Students.find().skip(5).limit(5).pretty();

22.List the names of all the students having "a" as the second letter in their names.
db.Students.find({"Fname":{$regex: "^.a"}},{_id:0})

23.List the clients who stay in a city Bardoli, whose first letter is "M".
db.Students.find({"City":"Bardoli","Fname":{$regex: "^M"}},{_id:0})

24.Count number of students in the collection.
db.Students.count()

25.Make the collection empty
db.Students.remove({})

----------------------------------------------------------------------------
Practical List 2

use bmiit2022

db.createCollection("Items")

db.Items.insertMany([
{"id":"TP01","name":"Talcom Powder","manufracturer":"Persona","price":40,"qty":45,"stockdate":"11-02-18","dealer":"Krishna Cosmetik"},
{"id":"FW05","name":"Face Wash","manufracturer":"Attitude","price":45,"qty":55,"stockdate":"15-04-18","dealer":"Surbhi Collection"},
{"id":"BS01","name":"Bath Soap","manufracturer":"Persona","price":40,"qty":45,"stockdate":"11-02-18","dealer":"Krishna Cosmetik"},
{"id":"SH06","name":"Shampoo","manufracturer":"Shampoo","price":12,"qty":50,"stockdate":"05-01-17","dealer":"Surbhi Collection"},
{"id":"FW12","name":"Face Wash","manufracturer":"Loreal","price":95,"qty":45,"stockdate":"05-01-16","dealer":"Surbhi Collection"},
{"id":"S5005","name":"Ball Pen 0.5","manufracturer":"Nataraj Classic","price":16,"qty":100,"stockdate":"31-03-10","dealer":"Clear Deals"},
{"id":"S5003","name":"Ball Pen 0.25","manufracturer":"Nataraj Classic","price":20,"qty":150,"stockdate":"01-01-10","dealer":"Clear Deals"},
{"id":"S5002","name":"Gel Pen Premium","manufracturer":"Pilot Juice","price":14,"qty":125,"stockdate":"14-02-10","dealer":"Reliable Stationers"},
{"id":"S5006","name":"Gel Pen Classic","manufracturer":"Pilot Juice","price":22,"qty":200,"stockdate":"01-01-10","dealer":"Reliable Stationers"},
{"id":"S5001","name":"Eraser Small","manufracturer":"Apsara","price":15,"qty":210,"stockdate":"19-03-09","dealer":"Clear Deals"},
{"id":"S5004","name":"Eraser Big","manufracturer":"Apsara","price":10,"qty":60,"stockdate":"12-12-09","dealer":"Clear Deals"},
{"id":"S5009","name":"Sharper Classic","manufracturer":"Nataraj Classic","price":18,"qty":160,"stockdate":"23-01-09","dealer":"Clear Plastics"},
])

db.Items.find().pretty();

1.Display only the name and price of items, whose manufacturer is “Persona”.
db.Items.find({"manufracturer":"Persona" }, { "name" : 1 ,"price" : 1 , _id:0 } );

2.Count the total number of manufacturers.
db.Items.distinct("manufracturer").length

Count Products from Persona
db.Students.count({"manufracturer":"Persona"});

3.Display all the item’s details, whose names starts from "Sh".
db.Items.find({"name":{$regex: "^Sh"}},{_id:0})

4.Display the items details of all different types of pens.
db.Items.find({"name":{$regex: ".Pen."}},{_id:0})

5.Display only the name and price of product along with quantity, whose price is more than 80
db.Items.find({"price":{$gt:80}},{"name":1,"price":1,_id:0}).pretty()

6.Show only the item and its price of those items, whose price is less equal to 20.
db.Items.find({"price":{$lte:20}},{"name":1,"price":1,_id:0}).pretty()

7.Display the details of those items; whose dealer code is 102 or quantity is more than 100.
db.Items.find({$or: [{"id":"102"},{"qty":{$gt:100}}]})

8.Show the details of items, whose quantity is in the range of 50 to 100.
db.Items.find({$and:[{"qty":{$gt:50}},{"qty":{$lt:100}}]})

9.Increase the price of items by 10.
db.Items.updateMany({},{$inc:{"price":10}});

10.Decrease the quantity of items by 30.
db.Items.updateMany({},{$inc:{"qty":-30}});

11.Update the price of items by increasing 15%.
db.Items.updateMany({},{$mul:{"price":1.15}});

12.Find the maximum and minimum price of item in a single query.
db.Items.aggregate({$group:{_id:0,min:{$min:'$price'},max:{$max:'$price'}}});

13.Calculate the average of item price.
db.Items.aggregate({$group:{_id:0,avg_price:{$avg:'$price'}}});

14.Calculate the total amount of items and find the average of it.
db.Items.aggregate({$group:{_id:0,total_price:{$sum:'$price'},avg_price:{$avg:'$price'}}})

15.Display all item names and its price in following format: Talcom Powder price is 40. [Hint: $concat]
db.Items.aggregate({$project:{_id:0,msg:{$concat:["$name","price is ",{ $toString: "$price" }]}}})


16. Display all item names and its stock date in following format: The Ball Pen 0.5 stock date is 2010-03-31.
db.Items.aggregate({$project:{_id:0,msg:{$concat:["The","$name"," stock date is ",stockdate:{$dateToString: { format: "%Y-%m-%d", date: "$stockdate"}}]}}})

db.Items.aggregate({$project:{stockdate:{$dateToString:{format:"%Y-%m-%d", date:"$stockdate"}}}})


17. Show all item names with its price and stockdate. [Note: The “stoctdate” format must be as 21 st November 2019]
18. Arrange all the item’s details according to the item in ascending order.


19. Display details of all stock items in decreasing order of StockDate.
db.Items.aggregate([{$sort:{"stockdate":1}}]);

20. Find the manufacturer whose dealer is not Reliable Stationers, Clear Deals and Krishna Cosmetik.
db.items.find({$nor:[{"manufracturer":"Reliable Stationers"},{"manufracturer":"Clear Deals"},{"manufracturer":"Krishna Cosmetik"}]});

21. Show all the items in upper case. [Hint: $toUpper]
db.Items.aggregate(
[
{
$project:
{
name: { $toUpper: "$name" }
}
}
]
)

22. Display only the item names with its length. [Hint: { $strLenCP: "$<field_name>" }
db.items.find({},{name:1,length:{ $strLenCP: "$name" }})

23. Show all the manufacturers of items in small letters.
db.Items.aggregate(
[
{
$project:
{
manuracturer: { $toLower: "$manufracturer" }
}
}
]
)

24. Count the total number of dealers from items collection.
db.Items.distinct("dealer").length;

25. Count total number of items of different dealers.
db.Items.aggregate([{"$group" : {_id:"$dealer", count:{$sum:1}}}])

26. Display the maximum price of items for each dealer.
db.Items.aggregate(
[
{
$group:
{
_id: "$dealer",
maxprice: { $max: "$price" },
count:{$sum:1}
}
}
]
)

27. Find the total amount price of items for the date “01-01-2010”.


28. Extend the stockdate of items by two months.
db.Items.insert({date: ISODate('2022-12-07')});
db.Items.aggregate(
[
{
$project:
{
stockdate:
{
$dateAdd:
{
startDate: "$date",
unit: "month",
amount: 2
}
}
}
}
]
)


29. Find the manufacturer(s) whose item price is the lowest among all the items.
db.Items.aggregate(
[
{
$group:
{
minPrice: { $min: "$price" },
_id: "$manufracturer"
}
}
]
)

30. Delete all the records from all the collections.


----------------------------------------------------------------------------------------------------------

PRACTICAL LIST 3


using database named “bmiit2021” in MongoDB, create the collection called “Books” containing documents with some or
all of the following fields: bookId, bookTitle, authors (containing fields: authorName), publicationYear, publisher,
Orders (containing fields: OrderedId, orderDate, customerName, price, quantityOrdered, discount).
Note that a book may have one or more authors and orders. Also, the same Ordered can be present in one or more books.
Perform the following operations on the database (either in the console or using any programming language):

db.createCollection("Books")

1. Insert records for 10 books from 5 authors, and at least 20 orders in total.

db.Books.insertMany([
{bookid:1,title:"Harry Potter",authors:["J.K. Rowling"],publicationyear:2000,publisher:"London Publicaitn",orders:}



]);
==========================================================================================================================
mongoimport --db cietest --collection Items --file newdbexport.json

mongoexport --db cie -c Items --out newdbexport.json

==========================================================================================================================
map reduce

var mapfunction = function(){emit{this.date,this.quantityordered}}
var reducefuntion = function(key,values){return Array.sum(quantityordered)}
db.Books.mapReduce(mapfunction,reducefunction,{'out':'result'});

var mapfunction = function(){emit{this.customername,this.discount}}
var reducefuntion = function(key,values){return Array.avg(discount)}
db.Books.mapReduce(mapfunction,reducefunction,{'out':'result'});
============================================================================================================================
db.persons.aggregate([
{ $match: { gender: 'female' } },
{ $group: { _id: { state: "$location.state" }, totalPersons: { $sum: 1 } } },
{ $sort: { totalPersons: -1 } }
]).pretty();



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.