Module 4
Module 4
22AML64A
Next-Gen Database Technology using MongoDB
Module 4
Create Operations
• Create or insert operations add new documents to a collection. If the
collection does not currently exist, insert operations will create the collection.
• db.collection.insertOne()
• db.collection.insertMany()
Insert Operation : Single document
https://www.mongodb.com/docs/manual/reference/method/db.collection.insertOne/
Insert Operation : Single document
• In MongoDB, insert operations target a single collection. All write operation in
MongoDB are atomic on the level of a single document
• db.collection.insertOne() inserts a single document into a collection.
Ex.
db.inventory.insertOne(
{ item: "canvas",
qty: 100,
tags: ["cotton"],
size: { h: 28, w: 35.5}
})
• To retrieve the document that you just inserted
db.inventory.find( { item: "canvas" } )
Insert Multiple Documents(Batch
Insert)
Syntax
• The insertMany() method has the following syntax:
https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/
Insert Multiple Documents
https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite/#mongodb-method-db.collection.b
ulkWrite
• db.students.bulkWrite([
{ insertOne:
{“document”:{studentId:4, studentName:”GeekD”, studentAge:24}}},
{insertOne:
{“document”:{studentId:4, studentName:”GeekD”, studentAge:24}}}
{ updateOne :
{ "filter" : { "studentId" : 2 },
"update" : { $set : { "studentName" : "GeekyBest" } }
}}
]);
Ordered Bulk Insert
• With an ordered list of operations, MongoDB executes the operations
serially.
• If an error occurs during the processing of one of the write
operations, MongoDB will return without processing any remaining
write operations in the list
• Bulk()
• Bulk operations builder used to construct a list of write operations to perform
in bulk for a single collection. To instantiate an ordered bulk operation use -
db.collection.initializeOrderedBulkOp() method
Performing Ordered Bulk Insert
• With an ordered operations list, MongoDB executes the write
operations in the list serially
• When executing an ordered list of operations, MongoDB groups the
operations by the operation type and contiguity; i.e. contiguous
operations of the same type are grouped together
• For example, if an ordered list has two insert operations followed by an
update operation followed by another insert operation-
• MongoDB groups the operations into three separate groups:
• first group contains the two insert operations,
• second group contains the update operation
• third group contains the last insert operation
Performing Ordered Bulk Insert
Unordered Bulk Insert
• When executing an unordered list of operations, MongoDB groups the
operations.
• With an unordered bulk operation, the operations in the list may be reordered to
increase performance.
• As such, applications should not depend on the ordering when performing
unordered bulk operations.
• Bulk()
• Bulk operations builder used to construct a list of write operations to perform
in bulk for a single collection. To instantiate an ordered bulk operation use -
db.collection.initializeUnorderedBulkOp() method.
https://www.mongodb.com/docs/manual/reference/method/db.collection.initializeUnorderedBulkOp/
Unordered Bulk Insert
Retrieving the documents
• db.collection.find() retrieves the documents of a collection
Exercise -1 : Student Database
Create database, Create collection, insert data, find, limit, sort, skip, distinct.
Create a student database with the fields: (USN, Sname, Sem, Branch, CGPA)
Solve the following queries –
• Display all the documents
• Display all the students in AIML
• Display all the students in ascending order
• Display first 5 students
• List the degree of student “XYZ"
• Display students details of 5,6,7 in descending order of percentage
• Display the number of students in CSE
• Display all the distinct degrees
• Display all the ISE students with CGPA greater than 6, but less than 7.5
• Display all the students in AIDS and in 6th Sem
Create a MongoDB database and collection to store student information (USN, Sname, Sem, Branch, CGPA). Perform the below
data manipulation and analysis tasks using MongoDB queries
a. Demonstrate insertion of multiple documents 2M
Solution:
db.students.insertMany([
{ USN: "USN001", Sname: "Alice", Sem: 5, Branch: "CSE", CGPA: 8.2 },
{ USN: "USN002", Sname: "Bob", Sem: 3, Branch: "AIML", CGPA: 7.5 },
{ USN: "USN003", Sname: "Charlie", Sem: 4, Branch: "ISE", CGPA: 6.8 },
{ USN: "USN004", Sname: "David", Sem: 6, Branch: "CSE", CGPA: 9.1 },
{ USN: "USN005", Sname: "Eva", Sem: 2, Branch: "AIML", CGPA: 7.0 }]);
https://www.mongodb.com/docs/manual/reference/operator/query/in/
$or
The $or operator performs a logical OR operation on an array of one or more
<expressions> and selects the documents that satisfy at least one of the
<expressions>
This query will select all documents in the inventory collection where either the quantity field value is less
than 20 or the price field value equals 10.
https://www.mongodb.com/docs/manual/reference/operator/query/or/
$or
$and
$and performs a logical AND operation on an array of one or more
expressions (<expression1>, <expression2>, and so on) and selects the
documents that satisfy all the expressions.
https://www.mongodb.com/docs/manual/reference/operator/query/and/
$and
Regular Expression
$regex
• Provides regular expression capabilities for pattern
matching strings in queries.
https://www.mongodb.com/docs/manual/reference/operator/query/regex/
$Where Query
• Use the $where operator to pass either a string containing a
JavaScript expression or a full JavaScript function to the query system.
• The $where provides greater flexibility, but requires that the database
processes the JavaScript expression or function for each document in
the collection.
• Reference the document in the JavaScript expression or function
using either this or obj
$Where Query -Example
json javascript
{ db.products.find({
"_id": 1, $where: function() {
"name": "Product A",
return this.price * this.quantity > 3000;
"price": 50,
"quantity": 100
}
} })
{
"_id": 2, In this example:
"name": "Product B", • this refers to the current document being
"price": 70, evaluated.
"quantity": 20 • The JavaScript function checks if the product's
} total value (price * quantity) exceeds 3000.
Advance query option
• Array query operators – https://www.mongodb.com/docs/manual/reference/operator/query-array/
• Projection query operators - https://www.mongodb.com/docs/manual/reference/operator/projection/
• Bitwise query operators – https://www.mongodb.com/docs/manual/reference/operator/query-bitwise/
• Geospatial query operators - https://www.mongodb.com/docs/manual/reference/operator/query-geospatial/
Update Operation
• update operations are used to modify existing documents in a
collection
• mongosh leverages the following methods for update operations –
Update Operators – Field Update Operators
• MongoDB provides several update operators to perform specific operations on fields: