Skip to content

SEC-MASUM/mongoose-practice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

mongoose Practice Aggregation

Example data:

[
   {
     "name": "John Doe",
     "email": "[email protected]",
     "age": 28,
     "address": {
       "street": "123 Main St",
       "city": "New York",
       "state": "NY",
       "zipcode": "10001"
     },
     "favorites": {
       "color": "blue",
       "food": "pizza",
       "movie": "The Shawshank Redemption"
     },
     "friends": [
       {
         "name": "Jane Smith",
         "email": "[email protected]"
       },
       {
         "name": "Mike Johnson",
         "email": "[email protected]"
       }
     ]
   },
   {
     "name": "Alice Williams",
     "email": "[email protected]",
     "age": 35,
     "address": {
       "street": "456 Elm St",
       "city": "San Francisco",
       "state": "CA",
       "zipcode": "94101"
     },
     "favorites": {
       "color": "green",
       "food": "sushi",
       "movie": "The Godfather"
     },
     "friends": [
       {
         "name": "Bob Anderson",
         "email": "[email protected]"
       },
       {
         "name": "Emily Davis",
         "email": "[email protected]"
       }
     ]
   }
 ]

Task 1: Find all users who are located in New York.

Task 2: Find the user(s) with the email "[email protected]" and retrieve their favorite movie.

Task 3: Find all users with "pizza" as their favorite food and sort them according to age.

Task 4: Find all users over 30 whose favorite color is "green".

Task 5: Count the number of users whose favorite movie is "The Shawshank Redemption."

Task 6: Update the zipcode of the user with the email "[email protected]" to "10002".

Task 7: Delete the user with the email "[email protected]" from the user data.

Task 8: Group users by their favorite movie and retrieve the average age in each movie group.

Task 9: Calculate the average age of users with a favorite " pizza " food.

// orders
[
  {
    "_id": 1,
    "order_number": "ORD-001",
    "customer_id": 1,
    "total_amount": 100.0
  },
  {
    "_id": 2,
    "order_number": "ORD-002",
    "customer_id": 2,
    "total_amount": 150.0
  },
  {
    "_id": 3,
    "order_number": "ORD-003",
    "customer_id": 1,
    "total_amount": 200.0
  }
]

// customers
[
  {
    "_id": 1,
    "name": "Alice Williams",
    "email": "[email protected]"
  },
  {
    "_id": 2,
    "name": "Bob Anderson",
    "email": "[email protected]"
  },
  {
    "_id": 3,
    "name": "Emily Davis",
    "email": "[email protected]"
  }
]

Task 10: Perform a lookup aggregation to retrieve the orders data along with the customer details for each order.

More Tasks on Aggregation

Task 1: Group users by their favorite color and retrieve the count of users in each color group.

Task 2: Find the user(s) with the highest age.

Task 3: Find the most common favorite food among all users.

Task 4: Calculate the total count of friends across all users.

Task 5: Find the user(s) with the longest name.

Task 6: Calculate each state's total number of users in the address field.

Task 7: Find the user(s) with the highest number of friends.

These tasks involve using various aggregation operators such as $group, $avg, $max, $sum, and $project to perform complex calculations and data transformations. You can write MongoDB aggregation queries to accomplish each task based on user data. Adjust the queries according to your specific implementation and requirements.

Releases

No releases published

Packages

No packages published