Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:31:17+00:00 2026-05-23T19:31:17+00:00

Part 1: I have (student) collection: { sname : , studentId: 123 age: ,

  • 0

Part 1:

I have (student) collection:

 {
    sname : "",
    studentId: "123"
    age: "",
    gpa: "",
 }

im trying to get only two keys from it :

{
    sname : "",
    studentId: "123"
}

so i need to eliminate age and gpa to have only name and studentId , how could i do that ?

Part2:

Then I have ‘subject’ collection :

{
    subjectName : "Math"
    studentId : "123"
    teacherName: ""
 }

I need to match/combine the previous keys (in part1) with the correct studentId so I will end up with something like this :

 {
    sname : "",
    studentId: "123",
    subjectName : "Math" 

 }

How can i do this and is that the right way to think to get the result? i tried to read about group and mapReduce but i didnt find a clear example.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:31:18+00:00Added an answer on May 23, 2026 at 7:31 pm

    To answer your first question, you can do this:

    db.student.find({}, {"sname":1, "studentId":1});
    

    The first {} in that is the limiting query, which in this case includes the entire collection. The second half specifies keys with a 1 or 0 depending on whether or not you want them back. Don’t mix include and excludes in a single query though. Except for a couple special cases, mongo won’t accept it.

    Your second question is more difficult. What you’re asking for is a join and mongo doesn’t support that. There is no way to connect the two collections on studentId. You’ll need to find all the students that you want, then use those studentIds to find all the matching subjects. Then you’ll need to merge the two results in your own code. You can do this through whatever driver you’re using, or you can do this in javascript in the shell itself, but either way, you’ll have to merge them with your own code.

    Edit:
    Here’s an example of how you could do this in the shell with the output going to a collection called “out”.

    db.student.find({}, {"sname":1, "studentId":1}).forEach(
      function (st) {
        db.subject.find({"studentId":st.studentId}, {"subjectName":1}).forEach(
          function (sub) {
            db.out.insert({"sname":st.sname, "studentId":st.studentId, "subjectName":sub.subjectName});
          }
        );
      }
    );
    

    If this isn’t data that changes all that often, you could just drop the “out” collection and repopulate it periodically with this shell script. Then your code could query directly from “out”. If the data does change frequently, you’ll want to do this merging in your code on the fly.

    Another, and possibly better, option is to include the “subject” data in the “student” collection or vice versa. This will result in a more mongodb friendly structure. If you run into this joining problem frequently, mongo may not be the way to go and a relational database may be better suited to your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app for android and i am trying to have part
See the excerpt below taken from a PL/SQL tutorial. The part I have an
I'm stuck on one thing i can't get solved. I have part of code,
I am a student, this is part of my homework. I have to Update
'm a student, just starting working with Haskell and have problems with part of
I have the following part of code : for(int i=0;i<n;i++) { printf(Student %dn,i+1); printf(Enter
I am a student trying to implement the DES algorithm. I have a choice
i have part of Asp.NET 1.1 project. I work with remote site, which works
I have part objects and material objects. Each part has at most 1 material
I'm using an update panel to have part of my page update asynchronously. Its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.