1a. Illustration of Where Clause, AND,OR operations in MongoDB.
//To create the new database as well as switch the database if not existing
use ProgrammingBooks
//To create the collection inside the database
db.createCollection("BookDetails")
Note: Insert single value or document in collection command…
//To insert the single value or document
db.BookDetails.insertOne({
_id: 1,
title: "Clean Code",
author: "Robert C. Martin",
category: "Software Development",
year: 2008
})
Note: Insert multiples values or document in collection command…
//To insert the multiple values or documents
db.BookDetails.insertMany([
{_id: 1, title: "Clean Code", author: "Robert C. Martin", category: "Software Development", year: 2008 },
{_id: 2, title: "JavaScript: The Good Parts", author: "Douglas Crockford", category: "JavaScript", year: 2008 },
{_id: 3, title: "Design Patterns", author: "Erich Gamma", category: "Software Design", year: 1994 },
{_id: 4, title: "Introduction to Algorithms", author: "Thomas H. Cormen", category: "Algorithms", year: 2009 },
{_id: 5, title: "Python Crash Course", author: "Eric Matthes", category: "Python", year: 2015 }
]);
Using the WHERE
Clause:
//Condition statement for where operator
db.BookDetails.find({ year: 2008 }).pretty()
OUTPUT:
[
{
_id: 1,
title: 'Clean Code',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: 2,
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
}
]
Using the $and
Operator:
//Condition statement for $and operator
db.BookDetails.find({
$and: [
{ category: "Software Development" },
{ year: 2008 }
]
}).pretty()
OUTPUT:
[
{
_id: 1,
title: 'Clean Code',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
}
]
Using the $or
Operator:
////Condition statement for $or operator
db.BookDetails.find({
$or: [
{ category: "JavaScript" },
{ year: 2015 }
]
}).pretty()
OUTPUT:
[
{
_id: 2,
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: 5,
title: 'Python Crash Course',
author: 'Eric Matthes',
category: 'Python',
year: 2015
}
]
1b. Execute the Commands of MongoDB and operations in MongoDB : Insert, Query, Update, Delete and Projection. (Note: use any collection)
//To create the new database as well as switch the database if not existing
use Books
//To create the collection inside the database
db.createCollection("BookDetails")
Note: Insert single value or document in collection command…
//To insert the single value or document
db.BookDetails.insertOne({
_id: 1,
title: "The Pragmatic Programmer: Your Journey to Mastery",
author: "David Thomas, Andrew Hunt",
category: "Software Development",
year: 1999
})
Note: Insert multiple values or document in collection command…
//To insert the multiple values or documents
db.BookDetails.insertMany([
{
_id: 1,
title: "Clean Code: A Handbook of Agile Software Craftsmanship",
author: "Robert C. Martin",
category: "Software Development",
year: 2008
},
{
_id: 2,
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
category: "JavaScript",
year: 2008
},
{
_id: 3,
title: "Design Patterns: Elements of Reusable Object-Oriented Software",
author: "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides",
category: "Software Design",
year: 1994
},
{
_id: 4,
title: "Introduction to Algorithms",
author: "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein",
category: "Algorithms",
year: 1990
},
{
_id: 5,
title: "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
author: "Eric Matthes",
category: "Python",
year: 2015
}
])
Query Operation:
1. Find All Documents command…
db.BookDetails.find().pretty()
OUTPUT:
[
{
_id: 1,
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: 2,
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: 3,
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Software Design',
year: 1994
},
{
_id: 4,
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'Algorithms',
year: 1990
},
{
_id: 5,
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
}
]
2. Find Documents Matching a Condition:
db.BookDetails.find({ year: { $gt: 2000 } }).pretty()
OUTPUT:
[
{
_id: 1,
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: 2,
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: 5,
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
}
]
Update Operations:
Note: Update single value or document in collection command…
//To insert the single value or document
db.BookDetails.updateOne(
{ title: "Clean Code: A Handbook of Agile Software Craftsmanship" },
{ $set: { author: "vtucode" } }
)
//To see the updated result
db.BookDetails.find({ year: { $eq: 2008 } }).pretty()
OUTPUT:
[
{
_id: 1,
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'vtucode',
category: 'Software Development',
year: 2008
},
{
_id: 2,
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
}
]
Note: Update multiple values or document in collection command…
//To insert the multiple values or documents
db.BookDetails.updateMany(
{ year: { $lt: 2010 } },
{ $set: { category: "vtucode website" } }
)
//To see the updated result
db.BookDetails.find({ year: { $lt: 2010 } }).pretty()
OUTPUT:
[
{
_id: 1,
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'vtucode',
category: 'vtucode website',
year: 2008
},
{
_id: 2,
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'vtucode website',
year: 2008
},
{
_id: 3,
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'vtucode website',
year: 1994
},
{
_id: 4,
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'vtucode website',
year: 1990
}
]
Delete Operations:
Note: Delete single value or document in collection command…
//To delete the single value or document
db.BookDetails.deleteOne({ _id: 2 })
//To verify the deleted document
db.BookDetails.find({ _id: 2 }).pretty()
Note: Delete multiple values or document in collection command…
//To delete the multiple values or documents
db.BookDetails.deleteMany({ year: { $lt: 1995 } })
//To verify the deleted document
db.BookDetails.find().pretty()
OUTPUT:
[
{
_id: 1,
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'vtucode',
category: 'vtucode website',
year: 2008
},
{
_id: 5,
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
}
]
Note: Delete all values or document in collection command…
//To delete the all values or document
db.BookDetails.deleteMany({ })
//To verify the deleted document
db.BookDetails.find().pretty()
Projection Operations:
Note: To retrieve specific include field values or document in collection command…
//To retrieve specific include field values or document
db.ProgrammingBooks.find({}, { title: 1, author: 1 })
OUTPUT:
[
{
_id: 1,
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin'
},
{
_id: 2,
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford'
},
{
_id: 3,
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides'
},
{
_id: 4,
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein'
},
{
_id: 5,
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes'
}
]
Note: To retrieve specific exclude field values or document in collection command…
//To retrieve specific exclude field values or document
db.BookDetails.find({}, { year:0 } )
OUTPUT:
[
{
_id: 1,
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development'
},
{
_id: 2,
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript'
},
{
_id: 3,
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Software Design'
},
{
_id: 4,
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'Algorithms'
},
{
_id: 5,
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python'
}
]