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 9192395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:54:44+00:00 2026-06-17T20:54:44+00:00

I read up that you can make Mongoose auto pouplate ObjectId fields. However I

  • 0

I read up that you can make Mongoose auto pouplate ObjectId fields. However I am having trouble structuring a query to populate fields in a subdoc.

My models:

var QuestionSchema = new Schema({
    question_text: String,
    type: String,
    comment_field: Boolean,
    core_question: Boolean,
    identifier: String
});

var SurveyQuestionSchema = new Schema({
    question_number: Number,
    question: {type: Schema.Types.ObjectId, ref: 'Question', required: true} //want this popuplated
});

var SurveySchema = new Schema({
    start_date: Date,
    end_date: Date,
    title: String,
    survey_questions: [SurveyQuestionSchema]
});

Right now I achieve the effect by doing:

Survey.findById(req.params.id, function(err, data){
    if(err || !data) { return handleError(err, res, data); }

    var len = data.survey_questions.length;
    var counter = 0;

    var data = data.toJSON();

    _.each(data.survey_questions, function(sq){
        Question.findById(sq.question, function(err, q){
            sq.question = q;

            if(++counter == len) {
                res.send(data);
            }
        });
    });
});

Which obviously is a very error-prone way of doing it…

  • 1 1 Answer
  • 0 Views
  • 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-06-17T20:54:47+00:00Added an answer on June 17, 2026 at 8:54 pm

    As I noted in the comments above, this is an issue currently under scrutiny by the mongoose team (not yet implemented).

    Also, looking at your problem from an outsider’s perpsective, my first thought would be to change the schema to eliminate SurveyQuestion, as it has a very relational db “join” model feel. Mongoose embedded collections have a static sort order, eliminating the need for keeping a positional field, and if you could handle question options on the Survey itself, it would reduce the schema complexity so you wouldn’t need to do the double-populate.

    That being said, you could probably reduce the queries down to 2, by querying for all the questions at once, something like:

    Survey.findById(req.params.id, function(err, data){
        if(err || !data) { return handleError(err, res, data); }
    
        var data = data.toJSON();
        var ids = _.pluck(data.survey_questions, 'question');
    
        Question.find({_id: { $in: ids } }, function(err, questions) {
            _.each(data.survey_questions, function(sq) {
                sq.question = _.find(questions, function(q) { 
                    var id = q._id.toString();
                    return id == sq.question; 
                });
    
            });
            res.send(data);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, how can you make an ArrayList read-only (so that no one can
How can I make an interactive function that interactively read a key from the
I read on one site that you can make constant variables in JavaScript like:
I’ve read that you can disable (make physically unclickable) an HTML button simply by
I have read somewhere that hyperthreading can make 32-bit int (on a 32-bit processor)
While reading from a site a read that you can not make a global
I have read that you can do it, but would this really improve performance
I've read that there is no way that JQuery can download a file directly,
I like the new Dynamic keyword and read that it can be used as
I'm building a plugin system for my application. I've read that anyone can decomple

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.