0

this is my PostSchema creation in mongoose, I've made a reference to the users table in both my post as well as the comment.

 const PostSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
  },
  text: {
    type: String,
    required: true,
  },
  date: {
    type: Date,
    default: Date.now,
  },
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'users',
  },
  comment: [
    {
      user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'users',
      },
      text: {
        type: String,
        require: true,
      },
      date: {
        type: Date,
        default: Date.now,
      },
    },
  ],
});

What I was trying to do was have the post update the user element on ref, when I am calling it, to provide the user id and the name.

    const posts = await Post.find().populate('user', 'name');

Which worked with this, but it didn't do the same for each of the users ref in the comment. Did I do something wrong?

Edit:

const newComment = {
        user: user._id,
        text: req.body.text,
      };
      post.comment.unshift(newComment);
      await post.save();

This is the code for the comment addition, user._id is the objectId of the current loggedin user.

I even tried making

        user: user._id,

to

user: req.user.id,


const newPost = new Post({
        text: req.body.text,
        title: req.body.title,
        user: req.user.id,
      });

      await newPost.save();

This is the code for the newPost, again req.user.id is the ID of the current loggedin in.

Edit:

this is user schema

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  date: {
    type: Date,
    default: Date.now,
  },
});

module.exports = mongoose.model('users', userSchema);

2 Answers 2

0

Please see how the populate can be use

let articles = await Product.find({ _id: { $in: items } })
    .populate("brand")
    .populate("wood")
    .exec();
Story.
  find().
  populate({ path: 'fans', select: 'name' }).
  populate({ path: 'fans', select: 'email' });
// The above is equivalent to:
Story.find().populate({ path: 'fans', select: 'email' });

d

Sign up to request clarification or add additional context in comments.

6 Comments

It didn't work for me, I even tried doing what was suggested for multilevel population on mongoose docs.
Can you share the User Schema
i have added the Users Schema to the post @Adepitan Oriyomi
When Exporting your schema it should be singular use module.exports = mongoose.model('User', userSchema); instead user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', },
I didn't understand what you meant by that. I added in that code to create a reference to the users collection to populate the user field with the object Id from the user collection
|
0

I figured out the solution to this problem a while back but I thought I would just update it here.

const posts = await Post.find().populate({select:"comments":populate:{path:'users'}});

select picks the path from the schema, then we can nest another populate path to specify nested options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.