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 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...

  • 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...

  • 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 to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • The Ch07_ConstructCo database stores data for a consultingcompany that tracks all charges to projects. The...

    The Ch07_ConstructCo database stores data for a consulting company that tracks all charges to projects. The charges are based on the hours each employee works on each project. The structure and contents of the Ch07_ConstructCo database are shown in Figure P7.1.Note that the ASSIGNMENT table in Figure P7.1 stores the JOB_CHG_HOUR values as an attribute (ASSIGN_CHG_HR) to maintain historical accuracy of the data. The JOB_CHG_HOUR values are likely to change over time. In fact, a JOB_CHG_ HOUR change will be...

  • I am working on multi-table queries for my SQL class using a premade database, which I...

    I am working on multi-table queries for my SQL class using a premade database, which I have included screenshots of. I received assistance and was able to complete a good number of the queries but the bolded ones seem absolutely impossible to do?? How do I write a query for the bolded questions?? I've scoured my textbook and notes and cannot get anything I try to successfully produce results. 1. List all current reservations with the trip ID, customer’s first...

  • need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data...

    need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data file – It has: player ID, player first name, middle initial, last name soccer.txt – Player information file – It has: player ID, goals scored, number of penalties, jersey number output file - formatted according to the example provided later in this assignment a) Define a struct to hold the information for a person storing first name, middle initial, and   last name). b) Define...

  • The primary keys are underlined. The foreign keys are denoted by asterisks (*). Description of the...

    The primary keys are underlined. The foreign keys are denoted by asterisks (*). Description of the schema: • person — keeps track of the people who borrow books from the library. The attributes contain personal and contact information. • author — keeps track of personal information about authors. • publisher — keeps track of the publisher information. To keep it simple, most of the attributes have been truncated in the sample database. 1 trivial dependencies are things like X→X or...

  • Saved Question 1 (20 points) List the names of responsible parties along with the artist name...

    Saved Question 1 (20 points) List the names of responsible parties along with the artist name of the artist they are responsible for. select artists.artistname, mem Question 2 (20 points) List each artist name and a count of the number of members assigned to that artist AV Question 3 (20 points) List each title from the Title table along with the name of the studio where it was recorded, the name of the artist, and the number of tracks on...

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
ADVERTISEMENT