0% found this document useful (0 votes)
9 views16 pages

MongoDB Manual

Uploaded by

indukrishece
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
9 views16 pages

MongoDB Manual

Uploaded by

indukrishece
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 16

ACS COLLEGE OF ENGINEERING

DEPARTMENT OF CSE-Data Science

MongoDB BDS456B
Manual
(Effective from the academic year 2023-2024)
Semester –IV
Prepared By:
Mrs. Indumathi K
Tutor
Department of CSE- Data Science ACSCE, Bengaluru
MongoDB BDS456B CSE-Data Science

Experiment 1: a. Illustration of Where Clause, AND,OR operations in MongoDB.

C:\Users\Indu>mongosh

Current Mongosh Log ID: 662007b444c5203fe1117b7a

Connecting to:
mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&a
ppName=mongosh+2.2.4

Using MongoDB: 7.0.8

Using Mongosh: 2.2.4

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------

The server generated these startup warnings when booting

2024-04-16T10:13:50.721+05:30: Access control is not enabled for the database. Read


and write access to data and configuration is unrestricted

------

test> show dbs

admin 40.00 KiB

config 108.00 KiB

deptofCSE 31.43 MiB

local 40.00 KiB

products 80.00 KiB

test> use products

switched to db products

ACS College Of Engineering Page 2


MongoDB BDS456B CSE-Data Science

products> show collections

customer_details

product_details

products> db.product_details.find({name:'AC7 Phone'})

_id: 'ac7',

name: 'AC7 Phone',

brand: 'ACME',

type: 'phone',

price: 320,

rating: 4,

warranty_years: 1,

available: false

products> db.product_details.find({$and:[{name:'AC7 Phone'},{brand:'ACME'}]})

_id: 'ac7',

name: 'AC7 Phone',

brand: 'ACME',

type: 'phone',

price: 320,

rating: 4,

warranty_years: 1,

ACS College Of Engineering Page 3


MongoDB BDS456B CSE-Data Science

available: false

products> db.product_details.find({$or:[{name:'AC7 Phone'},{brand:'ACME'}]})

_id: 'ac3',

name: 'AC3 Phone',

brand: 'ACME',

type: 'phone',

price: 200,

rating: 3.8,

warranty_years: 1,

available: true

},

_id: 'ac7',

name: 'AC7 Phone',

brand: 'ACME',

type: 'phone',

price: 320,

rating: 4,

warranty_years: 1,

available: false

ACS College Of Engineering Page 4


MongoDB BDS456B CSE-Data Science

b. Execute the Commands of MongoDB and operations in MongoDB : Insert, Query,


Update, Delete and Projection. (Note: use any collection)

//Insert Query

products> db.product_details.insertOne({_id:'ac8',name:'AC8
Phone',brand:'ACME',type:'phone',price:2000,rating:4.0,warranty_years:2,available:true})

{ acknowledged: true, insertedId: 'ac8' }

products> db.product_details.find({_id:'ac8'})

_id: 'ac8',

name: 'AC8 Phone',

brand: 'ACME',

type: 'phone',

price: 2000,

rating: 4,

warranty_years: 2,

available: true

//Update Query

products> db.product_details.updateOne({name:'AC8 Phone'},{$set:{price:4000}})

acknowledged: true,

insertedId: null,

matchedCount: 1,

modifiedCount: 1,

ACS College Of Engineering Page 5


MongoDB BDS456B CSE-Data Science

upsertedCount: 0

products> db.product_details.find({_id:'ac8'})

_id: 'ac8',

name: 'AC8 Phone',

brand: 'ACME',

type: 'phone',

price: 4000,

rating: 4,

warranty_years: 2,

available: true

//Deletion

products> db.product_details.deleteOne({_id:'ac8'})

{ acknowledged: true, deletedCount: 1 }

products> db.product_details.find({_id:'ac8'})

products>

//projection

products> db.product_details.find({type:'tv'})

_id: ObjectId('507d95d5719dbef170f15c01'),

ACS College Of Engineering Page 6


MongoDB BDS456B CSE-Data Science

name: 'Cable TV Basic Service Package',

type: 'tv',

monthly_price: 50,

rating: 3.9,

term_years: 2,

cancel_penalty: 25,

sales_tax: true,

additional_tarriffs: [

{ kind: 'federal tarriff', amount: { percent_of_service: 0.06 } },

{ kind: 'misc tarriff', amount: 2.25 }

Experiment 2:

a. Develop a MongoDB query to select certain fields and ignore some fields of the
documents:

products> db.product_details.find({brand:'ACME'},{name:0,_id:0})

brand: 'ACME',

type: 'phone',

price: 200,

rating: 3.8,

ACS College Of Engineering Page 7


MongoDB BDS456B CSE-Data Science

warranty_years: 1,

available: true

},

brand: 'ACME',

type: 'phone',

price: 320,

rating: 4,

warranty_years: 1,

available: false

},

brand: 'ACME',

type: 'phone',

price: 2000,

rating: 4,

warranty_years: 2,

available: true

b. Develop a MongoDB query to display the first 5 documents:

products> db.product_details.find().limit(5)

_id: 'ac3',

ACS College Of Engineering Page 8


MongoDB BDS456B CSE-Data Science

name: 'AC3 Phone',

brand: 'ACME',

type: 'phone',

price: 200,

rating: 3.8,

warranty_years: 1,

available: true

},

_id: 'ac7',

name: 'AC7 Phone',

brand: 'ACME',

type: 'phone',

price: 320,

rating: 4,

warranty_years: 1,

available: false

},

_id: ObjectId('507d95d5719dbef170f15bf9'),

name: 'AC3 Series Charger',

type: [ 'accessory', 'charger' ],

price: 19,

rating: 2.8,

warranty_years: 0.25,

for: [ 'ac3', 'ac7', 'ac9' ]

ACS College Of Engineering Page 9


MongoDB BDS456B CSE-Data Science

},

_id: ObjectId('507d95d5719dbef170f15bfa'),

name: 'AC3 Case Green',

type: [ 'accessory', 'case' ],

color: 'green',

price: 12,

rating: 1,

warranty_years: 0

},

_id: ObjectId('507d95d5719dbef170f15bfb'),

name: 'Phone Extended Warranty',

type: 'warranty',

price: 38,

rating: 5,

warranty_years: 2,

for: [ 'ac3', 'ac7', 'ac9', 'qp7', 'qp8', 'qp9' ]

Experiment 3:

a. Execute query selectors (comparison selectors, logical selectors):

products> db.product_details.find({price:{$gt:200}})

ACS College Of Engineering Page 10


MongoDB BDS456B CSE-Data Science

_id: 'ac7',

name: 'AC7 Phone',

brand: 'ACME',

type: 'phone',

price: 320,

rating: 4,

warranty_years: 1,

available: false

},

_id: ObjectId('66290e738a958f3edf117b7b'),

name: 'AC3 Phone',

brand: 'ACME',

type: 'phone',

price: 2000,

rating: 4,

warranty_years: 2,

available: true

Experiment 4:

ACS College Of Engineering Page 11


MongoDB BDS456B CSE-Data Science

Create and demonstrate how projection operators ($, $elematch and $slice) would be
used in the MongoDB.

products> db.product_details.find({},{"name.$":1})

{ _id: 'ac3', name: 'AC3 Phone' },

{ _id: 'ac7', name: 'AC7 Phone' },

_id: ObjectId('507d95d5719dbef170f15bf9'),

name: 'AC3 Series Charger'

},

{ _id: ObjectId('507d95d5719dbef170f15bfa'), name: 'AC3 Case Green' },

_id: ObjectId('507d95d5719dbef170f15bfb'),

name: 'Phone Extended Warranty'

},

{ _id: ObjectId('507d95d5719dbef170f15bfc'), name: 'AC3 Case Black' },

{ _id: ObjectId('507d95d5719dbef170f15bfd'), name: 'AC3 Case Red' },

_id: ObjectId('507d95d5719dbef170f15bfe'),

name: 'Phone Service Basic Plan'

},

_id: ObjectId('507d95d5719dbef170f15bff'),

name: 'Phone Service Core Plan'

},

ACS College Of Engineering Page 12


MongoDB BDS456B CSE-Data Science

_id: ObjectId('507d95d5719dbef170f15c00'),

name: 'Phone Service Family Plan'

},

_id: ObjectId('507d95d5719dbef170f15c01'),

name: 'Cable TV Basic Service Package'

},

{ _id: ObjectId('66290e738a958f3edf117b7b'), name: 'AC3 Phone' }

products>
db.product_details.find({},{additional_tarriffs:{$elemMatch:{amount:2.25}}})

{ _id: 'ac3' },

{ _id: 'ac7' },

{ _id: ObjectId('507d95d5719dbef170f15bf9') },

{ _id: ObjectId('507d95d5719dbef170f15bfa') },

{ _id: ObjectId('507d95d5719dbef170f15bfb') },

{ _id: ObjectId('507d95d5719dbef170f15bfc') },

{ _id: ObjectId('507d95d5719dbef170f15bfd') },

{ _id: ObjectId('507d95d5719dbef170f15bfe') },

{ _id: ObjectId('507d95d5719dbef170f15bff') },

{ _id: ObjectId('507d95d5719dbef170f15c00') },

_id: ObjectId('507d95d5719dbef170f15c01'),

additional_tarriffs: [ { kind: 'misc tarriff', amount: 2.25 } ]

},

ACS College Of Engineering Page 13


MongoDB BDS456B CSE-Data Science

{ _id: ObjectId('66290e738a958f3edf117b7b') }

// $slice projection operator

products> db.product_details.find({},{name:1,brand:1,for:{$slice:1},_id:0})

{ name: 'AC3 Phone', brand: 'ACME' },

{ name: 'AC7 Phone', brand: 'ACME' },

{ name: 'AC3 Series Charger', for: [ 'ac3' ] },

{ name: 'AC3 Case Green' },

{ name: 'Phone Extended Warranty', for: [ 'ac3' ] },

{ name: 'AC3 Case Black', for: 'ac3' },

{ name: 'AC3 Case Red', for: 'ac3' },

{ name: 'Phone Service Basic Plan' },

{ name: 'Phone Service Core Plan' },

{ name: 'Phone Service Family Plan' },

{ name: 'Cable TV Basic Service Package' },

{ name: 'AC3 Phone', brand: 'ACME' }

ACS College Of Engineering Page 14


MongoDB BDS456B CSE-Data Science

5. Execute Aggregation operations ($avg, $min,$max, $push, $addToSet etc.).


students encourage to execute several queries to demonstrate various aggregation
operators)

//find $avg

products> db.product_details.aggregate([{$group:{_id:0,avgprice:{$avg:'$price'}}}])

[ { _id: 0, avgprice: 326.6875 } ]

//find $min

products> db.product_details.aggregate([{$group:{_id:0,minprice:{$min:'$price'}}}])

[ { _id: 0, minprice: 12 } ]

//find $max

products> db.product_details.aggregate([{$group:{_id:0,maxprice:{$max:'$price'}}}])

[ { _id: 0, maxprice: 2000 } ]

// $push

products> db.product_details.aggregate([{$group:{_id:0,allnames:{$push:'$name'}}}])

_id: 0,

allnames: [

'AC3 Phone',

'AC7 Phone',

'AC3 Series Charger',

'AC3 Case Green',

'Phone Extended Warranty',

'AC3 Case Black',

'AC3 Case Red',

'Phone Service Basic Plan',


ACS College Of Engineering Page 15
MongoDB BDS456B CSE-Data Science

'Phone Service Core Plan',

'Phone Service Family Plan',

'Cable TV Basic Service Package',

'AC3 Phone'

ACS College Of Engineering Page 16

You might also like