0% found this document useful (0 votes)
26 views5 pages

Extra 3

The document shows various aggregate queries on a students collection: 1) It finds the minimum, maximum, total and average ages of all students 2) It filters students in the Computer branch or aged 20 using $or 3) It filters students in Computer branch and aged 20 using $and 4) It filters students not in the Electrical branch using $not 5) It filters students neither in Computer branch nor aged 20 using $not and $or
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Extra 3

The document shows various aggregate queries on a students collection: 1) It finds the minimum, maximum, total and average ages of all students 2) It filters students in the Computer branch or aged 20 using $or 3) It filters students in Computer branch and aged 20 using $and 4) It filters students not in the Electrical branch using $not 5) It filters students neither in Computer branch nor aged 20 using $not and $or
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

shruti_college> db.students.

aggregate([
... {
... $group: {
... _id: null,
... minAge: { $min: "$age" }
... }
... }
... ]);
[ { _id: null, minAge: 20 } ]

shruti_college> db.students.aggregate([
... {
... $group: {
... _id: null,
... maxAge: { $max: "$age" }
... }
... }
... ]);
[ { _id: null, maxAge: 21 } ]

shruti_college> db.students.aggregate([
... {
... $group: {
... _id: null,
... totalAge: { $sum: "$age" }
... }
... }
... ]);
[ { _id: null, totalAge: 181 } ]

shruti_college> db.students.aggregate([
... {
... $group: {
... _id: null,
... averageAge: { $avg: "$age" }
... }
... }
... ]);
[ { _id: null, averageAge: 20.11111111111111 } ]

shruti_college> db.students.aggregate([
... {
... $match: {
... $or: [
... { branch: "Computer" },
... { age: 20 }
... ]
... }
... }
... ]);
[
{
_id: ObjectId("6536b7d68d4717ffe28229b4"),
name: 'Abhang',
age: 21,
branch: 'Computer'
},
{
_id: ObjectId("6536b7f38d4717ffe28229b5"),
name: 'Bhavana',
age: 20,
branch: 'Computer'
},
{
_id: ObjectId("6536b8718d4717ffe28229b7"),
name: 'Dinesh',
age: 20,
branch: 'EnTC'
},
{
_id: ObjectId("6536b8718d4717ffe28229b8"),
name: 'Eshwari',
age: 20,
branch: 'AI&DS'
},
{
_id: ObjectId("6536b8be8d4717ffe28229ba"),
name: 'Ganesh',
age: 20,
branch: 'EnTC'
},
{
_id: ObjectId("6536b8be8d4717ffe28229bb"),
name: 'Fatima',
age: 20,
branch: 'AI&DS'
},
{
_id: ObjectId("6536b8be8d4717ffe28229bc"),
name: 'Ishita',
age: 20,
branch: 'Mechanical'
},
{
_id: ObjectId("6536b9038d4717ffe28229bf"),
name: 'Komal',
age: 20,
branch: 'AI&DS'
},
{
_id: ObjectId("6536b9038d4717ffe28229c0"),
name: 'Jolly',
age: 20,
branch: 'Electrical'
}
]

shruti_college> db.students.aggregate([
... {
... $match: {
... $and: [
... { branch: "Computer" },
... { age: 20 }
... ]
... }
... }
... ]);
[
{
_id: ObjectId("6536b7f38d4717ffe28229b5"),
name: 'Bhavana',
age: 20,
branch: 'Computer'
}
]

shruti_college> db.students.aggregate([
... {
... $match: {
... branch: {
... $not: { $eq: "Electrical" }
... }
... }
... }
... ]);
[
{
_id: ObjectId("6536b7d68d4717ffe28229b4"),
name: 'Abhang',
age: 21,
branch: 'Computer'
},
{
_id: ObjectId("6536b7f38d4717ffe28229b5"),
name: 'Bhavana',
age: 20,
branch: 'Computer'
},
{
_id: ObjectId("6536b8718d4717ffe28229b7"),
name: 'Dinesh',
age: 20,
branch: 'EnTC'
},
{
_id: ObjectId("6536b8718d4717ffe28229b8"),
name: 'Eshwari',
age: 20,
branch: 'AI&DS'
},
{
_id: ObjectId("6536b8be8d4717ffe28229ba"),
name: 'Ganesh',
age: 20,
branch: 'EnTC'
},
{
_id: ObjectId("6536b8be8d4717ffe28229bb"),
name: 'Fatima',
age: 20,
branch: 'AI&DS'
},
{
_id: ObjectId("6536b8be8d4717ffe28229bc"),
name: 'Ishita',
age: 20,
branch: 'Mechanical'
},
{
_id: ObjectId("6536b9038d4717ffe28229bf"),
name: 'Komal',
age: 20,
branch: 'AI&DS'
}
]
shruti_college> db.students.aggregate([
... {
... $match: {
... $not: {
... $or: [
... { branch: "Computer" },
... { age: 20 }
... ]
... }
... }
... }
... ]);

You might also like