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);
malikite
  • 44
  • 5

2 Answers2

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

  • It didn't work for me, I even tried doing what was suggested for multilevel population on mongoose docs. – malikite May 15 '20 at 18:58
  • Can you share the User Schema – Adepitan Oriyomi May 16 '20 at 22:23
  • i have added the Users Schema to the post @Adepitan Oriyomi – malikite May 17 '20 at 15:34
  • When Exporting your schema it should be singular use module.exports = mongoose.model('User', userSchema); instead user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', }, – Adepitan Oriyomi May 19 '20 at 17:18
  • 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 – malikite May 30 '20 at 16:14
  • module.exports = mongoose.model('users', userSchema); this is your current export declaration , use this module.exports = mongoose.model('User', userSchema); instead and see the changes in the database document , and also update the referencing – Adepitan Oriyomi May 31 '20 at 22:29
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.

malikite
  • 44
  • 5