Question 1: Find the number of people who are male
[
{
$match:{
gender :"male"
}
},
{
$count:"Count"
}
]
Q2: Group authors by eye color and count the number of authors in each eye color group.
[
{
$group: {
_id: "$eyeColor",
count:{
$sum:1
}
}
},{
$sort: {
count: 1
}
}
]
Q3: Find the average age of female authors.
[
{
$match: {
gender: "female"
}
},
{
$group: {
_id: null,
averageAge:{
$avg: "$age"
}
}
}
]
Q4:Group authors by their eye color and, for each group, find the average age of authors and the number of authors.
[
{
$group: {
_id: "$eyeColor",
count: {
$sum: 1
},
avgAge:{
$avg:"$age"
}
}
}
]
Q5: Which country has most number of users
[
{
$group: {
_id: "$company.location.country",
count: {
$sum: 1
}
}
},
{
$sort: {
count:-1
}
},
{
$limit:1
}
]
Q6. Get the name and age with tag ‘velit’
[
{
$match: {
isActive:false,
tags:"velit"
}
},
{
$project: {
name:1,
age:1
}
}
]
Q7: $push. Categorise user based on their favorite fruit
[
{
$group: {
_id: "$favoriteFruit",
users: {
$push: "$name"
}
}
}
]
Q8: Lookups
[
{
$lookup: {
from: "authors",
localField: "author_id",
foreignField: "_id",
as: "author_details"
}
},
{
$addFields: {
author_details: {
$arrayElemAt:["$author_details",0]
}
}
}
]