0% found this document useful (0 votes)
8 views22 pages

NoSQL Database Queries and Operations

The document contains a series of MongoDB queries demonstrating various operations on NoSQL databases, including finding, updating, and aggregating data from collections like 'country', 'item', 'City', 'inventory', 'book', 'course', and 'regular_expression'. It showcases the use of different query operators such as $gt, $lt, $in, and $ne, as well as aggregation functions like $group and $sum. The document serves as a reference for performing CRUD operations and data manipulation in NoSQL databases.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

NoSQL Database Queries and Operations

The document contains a series of MongoDB queries demonstrating various operations on NoSQL databases, including finding, updating, and aggregating data from collections like 'country', 'item', 'City', 'inventory', 'book', 'course', and 'regular_expression'. It showcases the use of different query operators such as $gt, $lt, $in, and $ne, as well as aggregation functions like $group and $sum. The document serves as a reference for performing CRUD operations and data manipulation in NoSQL databases.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NoSQL Database MTCA13303 ET24MTCA032

[Link]({ population: { $gt: 100000 } })

OUTPUT:

pg. 1
NoSQL Database MTCA13303 ET24MTCA032

2. [Link]({ language: "English", continent: "Asia" })

3. [Link]({

capital: "Washington, D.C",

population: { $in: [200000, 100000] }

})

4. [Link]({ name: "Australia", population: 125960000 })

5. [Link]({ name: { $ne: "United States" }, name: "Australia" })

OUTPUT:

6. [Link]({

capital: "Dubai",

name: "UAE",

language: { $in: ["Arabic", "English"] }

})

pg. 2
NoSQL Database MTCA13303 ET24MTCA032

1. [Link]({ status: "D" })

2. [Link]({ status: { $in: ["A", "D"] } })

pg. 3
NoSQL Database MTCA13303 ET24MTCA032

{ _id: ObjectId("...1"), item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },

{ _id: ObjectId("...2"), item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },

{ _id: ObjectId("...3"), item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "cm" }, status: "D" },

{ _id: ObjectId("...4"), item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },

{ _id: ObjectId("...5"), item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }

3. [Link]({ status: "A", qty: { $lt: 30 } })

_id: ObjectId("...1"),

item: "journal",

qty: 25,

size: { h: 14, w: 21, uom: "cm" },

status: "A"

4. [Link]({ item: /^p/ })

{ _id: ObjectId("...3"), item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "cm" }, status: "D" },

{ _id: ObjectId("...4"), item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },

{ _id: ObjectId("...5"), item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }

5. [Link]({ "size.h": { $lt: 10 } })

pg. 4
NoSQL Database MTCA13303 ET24MTCA032

{ _id: ObjectId("...2"), item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },

{ _id: ObjectId("...3"), item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "cm" }, status: "D" }

[Link]({ qty: { $gt: 50 } })

{ _id: ObjectId("...3"), item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "cm" }, status: "D" },

{ _id: ObjectId("...4"), item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }

7. [Link]({ "[Link]": { $ne: "cm" }, qty: { $in: [50, 100] } })

{ _id: ObjectId("...2"), item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },

{ _id: ObjectId("...3"), item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "cm" }, status: "D" } // excluded
(uom=cm)

8. [Link]([

{ $group: { _id: "$status", count: { $sum: 1 } } }

])

{ _id: "A", count: 3 },

{ _id: "D", count: 2 }

[Link](

{ item: "journal" },

{ $set: { production_date: ISODate("2020-09-01") } }

pg. 5
NoSQL Database MTCA13303 ET24MTCA032

10. [Link]({

production_date: {

$gte: ISODate("2020-09-01"),

$lte: ISODate("2020-09-12")

})

11. [Link]({ production_date: ISODate("2020-10-10") })

12. [Link]({}, [

{ $set: { total_price: { $multiply: ["$qty", 10] } } }

])

13. [Link]({

production_date: ISODate("1920-09-15"),

total_price: { $gt: 500 }

})

14. [Link]({

total_price: { $gte: 200, $lte: 500 }

})

pg. 6
NoSQL Database MTCA13303 ET24MTCA032

1. [Link]({ capital: { $in: ["Washington, D.C", "Italy"] } })

pg. 7
NoSQL Database MTCA13303 ET24MTCA032

2. [Link]({

population: { $gt: 1000000 },

language: { $in: ["English", "Japanese", "Portuguese"] }

})

pg. 8
NoSQL Database MTCA13303 ET24MTCA032

3. [Link]({

population: { $lt: 30000000 },

continent: { $nin: ["Asia", "North America", "South America"] }

})

4. [Link]({

language: "Japanese",

continent: "Asia",

capital: { $ne: "Tokyo" }

})

5. [Link]({

name: "New Zealand",

capital: "Wellington",

continent: "Oceania",

language: "English",

population: 44491724,

geoLocation: {

longitude: 40,

latitude: 174

pg. 9
NoSQL Database MTCA13303 ET24MTCA032

})

6. [Link]({

"[Link]": { $gt: 0, $lt: 90 },

"[Link]": { $gt: 0, $lt: 100 }

}, { name: 1 })

7. [Link]({

continent: "Asia",

language: { $in: ["English", "Hindi"] }

}, { name: 1, geoLocation: 1 })

pg. 10
NoSQL Database MTCA13303 ET24MTCA032

pg. 11
NoSQL Database MTCA13303 ET24MTCA032

[Link]({ continent: "North America" })

2. [Link]().sort({ population: 1 })

pg. 12
NoSQL Database MTCA13303 ET24MTCA032

3. [Link]({ continent: "North America" }).sort({ population: 1 })

pg. 13
NoSQL Database MTCA13303 ET24MTCA032

4. [Link]([{ $group: { _id: "$continent", cities: { $push: "$$ROOT" } } }])

pg. 14
NoSQL Database MTCA13303 ET24MTCA032

5. [Link]([

{ $sort: { population: -1 } },

{ $group: { _id: "$continent", city: { $first: "$$ROOT" } } }

])

pg. 15
NoSQL Database MTCA13303 ET24MTCA032

1. [Link]({}, {
$set: {
location: { longitude: 77.5946, latitude: 12.9716 }
}
})
2. [Link]({}, { $rename: { "unit": "measure" } })

pg. 16
NoSQL Database MTCA13303 ET24MTCA032

3. [Link](
{ qty: { $lt: 2 } },
{ $set: { qty: 5 } }
)
4. [Link]({}, { $set: { status: "delivered" } })
5. [Link](
{ itemName: "Rice Bag" },
{ $set: { price: 50 }, $mul: { total: 50 } }
)

[Link](
{ itemName: "Wheat Flour" },
{ $set: { price: 40 }, $mul: { total: 40 } }
)

[Link](
{ itemName: "Sugar" },
{ $set: { price: 45 }, $mul: { total: 45 } }
)

pg. 17
NoSQL Database MTCA13303 ET24MTCA032

pg. 18
NoSQL Database MTCA13303 ET24MTCA032

1. [Link]({}, { $set: { print_year: 2007 } })

2. [Link]({}, { $rename: { "year": "publication_year" } })

3. [Link]({ publication_year: 2021 }, { $set: { edition: 5 } })

4. [Link]({ tags: "programming" }, { $set: { authors: "Vidhi Sharma" } })

5. [Link]().sort({ print_year: 1 })

6. [Link]().sort({ price: 1 }).limit(1)

7. [Link]().sort({ price: -1 }).limit(1)

8. [Link]("books")

pg. 19
NoSQL Database MTCA13303 ET24MTCA032

1. [Link]({ hours: { $all: [10, 11] } })

2. [Link]({ hours: { $size: 3 } })

3. [Link]({ hours: { $elemMatch: { $gt: 9, $lt: 15 } } })

4. [Link]({}, [
{ $set: { trainerFirst: { $arrayElemAt: [ { $split: ["$trainer", " "] }, 0 ] },
trainerLast: { $arrayElemAt: [ { $split: ["$trainer", " "] }, 1 ] } } }
])

pg. 20
NoSQL Database MTCA13303 ET24MTCA032

5. [Link]([
{ $match: { trainer: "Asha" } },
{ $project: { name: 1, totalHours: { $sum: "$hours" } } }
])

6. [Link]([

{ $match: { trainer: "Bina" } },

{ $group: { _id: "$trainer", totalFees: { $sum: "$fees" } } }

])

7 [Link]([

{ $project: { name: 1, maxHours: { $max: "$hours" }, minHours: { $min: "$hours" } } }

])

8. [Link]().sort({ fees: -1 }).limit(1)

9. [Link]([{ $group: { _id: null, avgFees: { $avg: "$fees" } } }])

pg. 21
NoSQL Database MTCA13303 ET24MTCA032

1. db.regular_expression.find({ position: /developer/i })

2. db.regular_expression.find({ position: /software engineer/i })

3. db.regular_expression.find({ name: /^B/ })

4. db.regular_expression.find({ name: /a$/ })

5. db.regular_expression.find({ name: /ta/i })

pg. 22

You might also like