Question

The name of my Collection is : People. I am just mentioning the example of data...

The name of my Collection is : People.

I am just mentioning the example of data of people:

> db.people.find().limit(1)

{ "_id" : ObjectId("5d7d87aafed1d209fd0ed58f"), "name" : { "last" : "Keyes", "first" : "Ella", "middle" : "Bella" }, "age" : 43, "gender" : "F", "address" : { "city" : "Lamesa", "state" : "TX" }, "skills" : [ "SQL", "Python", "Perl", "Julia" ] }

Could you please help me to solve below questions?

1) Find the number of people in each ten year age group (10 to 19, 20 to 29, etc.). Order the output by increasing age.

I have an answer for qeus-1, but it's not working!

db.people.aggregate([
       {"age":"$range: [ 0, 100, 10 ]" , count:{$sum:1}}
   ,
   {$sort:{"count":1}}
])

2)Count the number of people with each last name, print all last names and counts for which where is more than one person. Are there any duplicate first-last names in the collection?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. We have the ages saved already. All we need to do is add a new field range and place each age group in the corresponding age range. And then have the number of counts for each age range. So let us break down the problem.

For adding the new field range, hard code each range set with appropriate labels. Here I have hardcoded for ages 0 to 90. After creating the range, simply group them by range and return the count. Also, add a sort option for sorting

  

db,people.aggregate([

{

$project:{

"range":{

$concat:[

{ $cond: [{$and:[ {$gt:["$age", 0 ]}, {$lt: ["$age", 10]}]}, "Under 10", ""] },

{ $cond: [{$and:[ {$gte:["$age", 10 ]}, {$lt: ["$age", 20]}]}, "10-20", ""] },

{ $cond: [{$and:[ {$gte:["$age", 20 ]}, {$lt: ["$age", 30]}]}, "20-30", ""] },

{ $cond: [{$and:[ {$gte:["$age", 30 ]}, {$lt: ["$age", 40]}]}, "30-40", ""] },

{ $cond: [{$and:[ {$gte:["$age", 40 ]}, {$lt: ["$age", 50]}]}, "40-50", ""] },

{ $cond: [{$and:[ {$gte:["$age", 50 ]}, {$lt: ["$age", 60]}]}, "50-60", ""] },

{ $cond: [{$and:[ {$gte:["$age", 60 ]}, {$lt: ["$age", 70]}]}, "60-70", ""] },

{ $cond: [{$and:[ {$gte:["$age", 70 ]}, {$lt: ["$age", 80]}]}, "70-80", ""] },

{ $cond: [{$and:[ {$gte:["$age", 80 ]}, {$lt: ["$age", 90]}]}, "80-90", ""] },

]

}

}

},

{$sort::{"age":1}}

{

  $group: {

    "_id" : "$range",

    count: {

      $sum: 1

    }

  }

}

])

2. Counting the number of people with respect to their last and first names is also the same way. Use aggregate to group using the last name and return the count.

db.people.aggregate([

{

  $group: {

    "_id" : "$last",

    count: {

      $sum: 1

    }

  }

}

])

To check for duplicate first names project the first names based on condition that the count >1.

db.people.aggregate([

$project:{

"first":{

{ $cond: {[count:{$gt:1},

$group: {

    "_id" : "$first",

    count: {

      $sum: 1

    }

  }

]}

}

}

}

])

Add a comment
Know the answer?
Add Answer to:
The name of my Collection is : People. I am just mentioning the example of data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I need help answering all 8 of these questions for my BIS 422 class. I...

    Hello, I need help answering all 8 of these questions for my BIS 422 class. I need the appropriate MySQL script to use for these questions Provide the SQL for the following data requests. Your SQL should be written as a single script that can be pasted into MySQL and run without edits. Make sure you use the proper notation for your comments (see the practice solution for an example of the format). There will be a 5% deduction for...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • Im struggling with my final assignment and I don’t know how to start. The code has...

    Im struggling with my final assignment and I don’t know how to start. The code has to be done in python using tkinter module. I would very appreciate the help thank you QUESTION 1 Write a program that counts the number of names on a list. The program must include a class named FinalProjectlastName with the following attributes (class variables) and functions: Name: FinalProjectLastName Attributes: names: list of strings that stores names count: number of names on the list. Functions:...

  • My module page is correct but I don't believe I am using the main() function properly....

    My module page is correct but I don't believe I am using the main() function properly. One of my .py files is supposed to contain my definitions, which is the module file. My second .py file is supposed to properly call a main function that does what my program asks it to: First name, last name, age, and respond based off of input. For some reason I am struggling with using the main() function so that my program works. Any...

  • PLEASE DO THIS WITH PYTHON! Please do this with PYTHON! Assume the example of a name...

    PLEASE DO THIS WITH PYTHON! Please do this with PYTHON! Assume the example of a name list for all problems below (use it as a line in your code) e.g. nameist -ll'Julia Truong'.'Chen Wu'vJeb Castro,'Ron Kennedy', 'X YI 1. Write a recursive function convertNames0 to accept the initial nameList and return a new list containing only first names. Provide function call and print the resulting list after the function had returned the results. (solution similar to problem on the slide...

  • I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an ...

    I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an algorithm .. CSE 1311-Project 4 Part I: Create and print out the two arrays: (Be sure to do this first) You are allowed to hard code these arrays into your program. You can also put the data into a file and read the information into the program. The data is as follows: 150 250 Anne Bob Ralph 305...

  • IN PYTHON ONLY I am looking for 4 columns, Age, Gender, Ideal Age of a Spouse,...

    IN PYTHON ONLY I am looking for 4 columns, Age, Gender, Ideal Age of a Spouse, and the message. I will have 6 rows in the table, and 4 columns, followed by  averages. Calculate the ideal age of a spouse. Enter either m or f from the keyboard in lower case. You may use string data for the gender. Convert the gender to upper case Enter an age from the keyboard, probably an integer You will need prompts telling the user...

  • IN PYTHON ONLY I am looking for 4 columns, Age, Gender, Ideal Age of a Spouse, and the message. I will have 6 rows in th...

    IN PYTHON ONLY I am looking for 4 columns, Age, Gender, Ideal Age of a Spouse, and the message. I will have 6 rows in the table, and 4 columns, followed by  averages. Calculate the ideal age of a spouse. Enter either m or f from the keyboard in lower case. You may use string data for the gender. Convert the gender to upper case Enter an age from the keyboard, probably an integer You will need prompts telling the...

  • schema book: book_id, book_name, author_id. Author: author_id, first_name, last_name. DOB, social. i am trying to write...

    schema book: book_id, book_name, author_id. Author: author_id, first_name, last_name. DOB, social. i am trying to write a query that lists the first and last name of each author and counts how many books they wrote if they wrote more than one. but i am having issue . this is what i have so far, select book_name, count(b.book_name) as "number of books" from book b join author a on (b.author_id = a.author_id) group by b.book_name having count (a.author_id) >1;

  • Q1: The following question are based on these database tables: EMPLOYEE FNAME MIDINT LNAME SSN BDATE ADDRESS SEX SALARY SUPER_SSN Dept_No James A Borg 123123123 Hous...

    Q1: The following question are based on these database tables: EMPLOYEE FNAME MIDINT LNAME SSN BDATE ADDRESS SEX SALARY SUPER_SSN Dept_No James A Borg 123123123 Houston, TX M 55000 Null 1 Franklin S Wong 234234234 Houston, TX M 40000 123123123 5 John Q Smith 345345345 Houston, TX M 30000 234234234 5 Jennifer L Wallace 456456456 Bellaire, TX F 43000 123123123 4 Alicia M Zalaya 567567567 Spring, TX F 25000 456456456 4 Ramesh R Narayan 678678678 Humble, TX M 38000 234234234...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT