Mongo DB
Mongo DB
Mongo DB
(Affiliated to VTU, Belagavi, Approved by AICTE, New Delhi and Govt. of Karnataka)
Accredited by NBA, New Delhi (CSE, ISE, ECE, MECH, CIVIL), NAAC-‘A’ Grade
Rajanukunte, Bengaluru – 560 064
Tel: 080-2846 8196, email: hodds@saividya.ac.in web: www.saividya.ac.in
VISION
MongoDB
(BDSL456B)
(As per Visvesvaraya Technological University Syllabus)
Compiled by:
Trademark
Edition: 2023-24
Document Owners
The primary contacts for questions regarding this document are:
1. Amarnath.patil@savidya.ac.in
Contact email id:
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Program Outcomes
PSO 1:Apply the skills of core computer science engineering, artificial intelligence, machine
learning, deep learning to solve futuristic problems.
PSO 2:Demonstrate computer knowledge, practical competency and innovative ideas in
computer science engineering, artificial intelligence and machine learning using
machine tools and techniques.
MongoDB | Amarnath Patil
Sl Experiments
No
1 a. Illustration of Where Clause, AND,OR operations in MongoDB.
b. Execute the Commands of MongoDB and operations in MongoDB : Insert,
Query, Update, Delete and Projection. (Note: use any collection)
2 a. Develop a MongoDB query to select certain fields and ignore some fields of the
documents from any collection.
b. Develop a MongoDB query to display the first 5 documents from the results
obtained in a. [use of limit and find]
3 a. Execute query selectors (comparison selectors, logical selectors ) and list
out the results on any collection
b. Execute query selectors (Geospatial selectors, Bitwise selectors ) and list out
the results on any collection
4 Create and demonstrate how projection operators ($, $elematch and $slice) would
be used in the MondoDB.
5 Execute Aggregation operations ($avg, $min,$max, $push, $addToSet etc.).
students encourage to execute several queries to demonstrate various aggregation
operators)
6 Execute Aggregation Pipeline and its operations (pipeline must contain $match,
$group, $sort, $project, $skip etc. students encourage to execute several queries to
demonstrate various aggregation operators)
7 a. Find all listings with listing_url, name, address, host_picture_url in the listings
And Reviews collection that have a host with a picture url
b. Develop queries to illustrate excluding documents with certain words and phrases
10 Develop an aggregation pipeline to illustrate Text search on Catalog data collection.
DATABASE
1. Data Model:
o RDBMS: RDBMS uses a structured data model based on tables
with rows and columns. Data is organized into tables with
predefined schemas, and relationships between tables are
established using foreign keys.
o Document Database: Document databases use a semi-
structured data model where data is stored in documents,
typically in formats like JSON or BSON. Each document can
have its own structure, and there is no fixed schema across the
entire database.
2. Schema:
o RDBMS: RDBMS typically requires a predefined schema, where
the structure of each table and the relationships between tables
must be specified before inserting data. Changes to the schema
can be complex and often require downtime.
o Document Database: Document databases are schema-less or
schema-flexible. Documents within the same collection
(equivalent to a table in RDBMS) can have different structures,
and new fields can be added dynamically without requiring a
predefined schema.
3. Query Language:
o RDBMS: RDBMS typically uses SQL (Structured Query
Language) for querying and manipulating data. SQL is powerful
and standardized across most relational databases.
o Document Database: Document databases often use query
languages specific to the database, such as MongoDB's query
language or Couchbase's N1QL. These languages are designed
to work with semi-structured data and allow for querying nested
fields within documents.
4. Scaling:
o RDBMS: Scaling RDBMS horizontally (across multiple servers)
can be challenging, especially for large-scale applications.
Vertical scaling (increasing the resources of a single server) is a
common approach for RDBMS.
o Document Database: Document databases are often designed to
scale horizontally with ease. They can distribute data across
Introduction to MongoDB:
Features:
Components:
Use Cases:
1. Basic Queries:
o Find: The find() method retrieves documents from a collection. It
takes a query object as a parameter to specify selection criteria.
Projection:
Query Operators:
``Sorting:
• The sort() method sorts documents in the result set based on specified
criteria.
Limiting Results:
Skipping Results:
• db.collection_name.find().skip(number)
• Example: db.users.find().skip(5) skips the first 5 users.
Indexing:
• db.collection_name.createIndex({ field: 1 })
• Example: db.users.createIndex({ name: 1 }) creates an index on the
name field.
Aggregation:
➢ Text Search:
Geospatial Queries:
db.collection_name.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [longitude, latitude] },
$maxDistance: distance_in_meters
}
}
})
Example:
db.places.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [-73.97, 40.77] },
$maxDistance: 1000
}
}
Experiments
Experiment 1:
// Insert document
db.collection.insertOne({ name: "John", age: 35, gender: "male" })
// Query document
db.collection.find({ age: { $gt: 30 } })
// Update document
db.collection.updateOne({ name: "John" }, { $set: { age: 40 } })
// Delete document
db.collection.deleteOne({ name: "John" })
// Projection
db.collection.find({}, { name: 1, age: 1 })
Experiment 2:
Experiment 3:
// Comparison selectors
db.collection.find({ age: { $gt: 30 } })
// Logical selectors
db.collection.find({ $and: [{ age: { $gt: 25 } }, { gender: "male" }] })
// Geospatial selectors
db.collection.find({ location: { $near: { $geometry: { type: "Point", coordinates:
[ -73.9667, 40.78 ] }, $maxDistance: 1000 } } })
// Bitwise selectors
db.collection.find({ flags: { $bitsAllSet: 4 } })
Experiment 4:
// $ projection operator
db.collection.find({}, { "grades.$": 1 })
Experiment 5:
// $avg
db.collection.aggregate([{ $group: { _id: null, avgAge: { $avg: "$age" } } }])
// $min
db.collection.aggregate([{ $group: { _id: null, minAge: { $min: "$age" } } }])
// $max
db.collection.aggregate([{ $group: { _id: null, maxAge: { $max: "$age" } } }])
// $push
db.collection.aggregate([{ $group: { _id: null, allNames: { $push: "$name" } }
}])
// $addToSet
db.collection.aggregate([{ $group: { _id: null, uniqueNames: { $addToSet:
"$name" } } }])
Experiment 6:
// Aggregation Pipeline
db.collection.aggregate([
{ $match: { age: { $gt: 30 } } },
{ $group: { _id: "$gender", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
{ $skip: 2 },
{ $project: { _id: 0, gender: "$_id", count: 1 } }
])
Experiment 7:
db.eCommerce.aggregate([
{ $group: { _id: "$product_id", avgRating: { $avg: "$rating" }, totalReviews: {
$sum: 1 } } }
])
Experiment 8:
// Unique index
db.collection.createIndex({ username: 1 }, { unique: true })
// Sparse index
db.collection.createIndex({ city: 1 }, { sparse: true })
// Compound index
db.collection.createIndex({ age: 1, city: -1 })
// Multikey index
db.collection.createIndex({ tags: 1 })
// Using index
db.collection.find({ username: "john" }).hint({ username: 1 })
Experiment 9:
Experiment10:
Develop an aggregation pipeline to illustrate Text search on Catalog data
collection
db.Catalog.aggregate([
$search: {
text: {
},
$project: {
])