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

  • Home
  • SEARCH
  • 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 8717917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:24:23+00:00 2026-06-13T06:24:23+00:00

I am adding a comment to an item.comments list. I need to get the

  • 0

I am adding a comment to an item.comments list. I need to get the comment.created_by user data before I output it in the response. How should I do this?

    Item.findById(req.param('itemid'), function(err, item){
        var comment = item.comments.create({
            body: req.body.body
            , created_by: logged_in_user
        });

        item.comments.push(comment);

        item.save(function(err, item){
            res.json({
                status: 'success',
                message: "You have commented on this item",

//how do i populate comment.created_by here???

                comment: item.comments.id(comment._id)
            });
        }); //end item.save
    }); //end item.find

I need to populate the comment.created_by field here in my res.json output:

                comment: item.comments.id(comment._id)

comment.created_by is a user reference in my mongoose CommentSchema. It currently is only giving me a user id, I need it populated with all the user data, except for password and salt fields.

Here is the schema as people have asked:

var CommentSchema = new Schema({
    body          : { type: String, required: true }
  , created_by    : { type: Schema.ObjectId, ref: 'User', index: true }
  , created_at    : { type: Date }
  , updated_at    : { type: Date }
});

var ItemSchema = new Schema({
    name    : { type: String, required: true, trim: true }
  , created_by  : { type: Schema.ObjectId, ref: 'User', index: true }
  , comments  : [CommentSchema]
});
  • 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-13T06:24:24+00:00Added an answer on June 13, 2026 at 6:24 am

    In order to populate referenced subdocuments, you need to explicitly define the document collection to which the ID references to (like created_by: { type: Schema.Types.ObjectId, ref: 'User' }).

    Given this reference is defined and your schema is otherwise well defined as well, you can now just call populate as usual (e.g. populate('comments.created_by'))

    Proof of concept code:

    // Schema
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    
    var UserSchema = new Schema({
      name: String
    });
    
    var CommentSchema = new Schema({
      text: String,
      created_by: { type: Schema.Types.ObjectId, ref: 'User' }
    });
    
    var ItemSchema = new Schema({
       comments: [CommentSchema]
    });
    
    // Connect to DB and instantiate models    
    var db = mongoose.connect('enter your database here');
    var User = db.model('User', UserSchema);
    var Comment = db.model('Comment', CommentSchema);
    var Item = db.model('Item', ItemSchema);
    
    // Find and populate
    Item.find({}).populate('comments.created_by').exec(function(err, items) {
        console.log(items[0].comments[0].created_by.name);
    });
    

    Finally note that populate works only for queries so you need to first pass your item into a query and then call it:

    item.save(function(err, item) {
        Item.findOne(item).populate('comments.created_by').exec(function (err, item) {
            res.json({
                status: 'success',
                message: "You have commented on this item",
                comment: item.comments.id(comment._id)
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textarea, where the user can add their comments. Now after adding
I'm adding some user feedback mechanism into my app. The user types some comments
I have article model with public ICollection<Comment> Comments { get; set; } and comment
Adding a comment such as this: // TODO: Refactor this code ...creates a task
On my ASPX page I've added Dropdownlist. Elements in this list are divided to
I would like to display thank you message after adding comment only for the
Using Spring 3 I have two forms: adding Item and invalidating Item I have
I have been banging my head on adding a button to a cell. This
I've got a page with a list of some items. Each item has got
I'm loading a TreeView from a list, and the user has a button to

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.