0
let answer_Schema = mongoose.Schema({
    userID: { type: String, require: true },
    answer: { type: String, require: true },
    comments: [comment_Schema],
    profileID: { type: mongoose.Schema.Types.ObjectId, ref: "sign-ups" }
})

comment_Schema = {
  profileID: { type: mongoose.Schema.Types.ObjectId, ref: "sign-ups" },
  comment: { type: String },
  subcomments: [comment_Schema],
  dateTime: { type: String },
  likes: [likes_Schema],
  level: { type: Number }
}

this is the comment schema now this recursive model may go as deep as 5 levels, so it may look something like this:

{
  answer: "SOME RANDOM ANSWER",
  profileID:"5d8240ac0750a31f183d4016"
  comments: [
    {
      _id: "5d93713d1c9d4400005230e5",
      profileID :"614c7a75403a5636b4029f28",
      comment : "klm",
      subcomments : [
        {
          _id: "5d93713d1c9d4400005230r4",
          profileID :"614c7a75403a5636b4029f28",
          comment : "xyz",
          subcomments : [
            {
              _id: "5d93713d1c9d4400005230456",
              profileID :"614c7a75403a5636b4029f28",
              comment : "wxy",
              subcomments : [
                {
                  _id: "5d93713d1c9d4400005230k7",
                  profileID :"614c7a75403a5636b4029f28",
                  comment : "abc",
                  subcomments : []
                }
              ],
            }
          ],
        }
      ],
    }
  ],
}

what i want here is to populate profileID from each comment even if it is deeply nested also including the profileID from answer itself

answers
    .findOne()
    .populate("questionID")
    .populate("profileID")
    .populate("comments.profileID")
    .populate("comments.subcomments.profileID")
    .populate("comments.subcomments.subcomments.profileID")
    .populate("comments.subcomments.subcomments.subcomments.profileID")
    .populate("comments.subcomments.subcomments.subcomments.subcomments.profileID")

this is what i have tried.

i have looked up at this process but i couldn't implement this to my requirements.

const userSchema = new Schema({
  name: String,
  friends: [{ type: ObjectId, ref: 'User' }]
});

can someone pls just guide me on this, thnks would appreciate it.

Ali Abyer
  • 1
  • 1

1 Answers1

0

Unless I'm mistaken it seems you're trying to call find() off of a Schema object. It should be using a Model instead. As for the actual structuring of they populate I think this post outlines it.

Edit: given your updated question it might be a bit more tricky because you have comments and subcomments. I think you can make your populate looks something like this:

answers.findOne()
  .populate({
    path: "profileID",
    populate: {
      path: "comments.profileID",
      populate: {
        path: "subcomments.profileID"
      }
    }
})

If you were able to modify the schema to be a bit more consistent then it would look a bit more like the second code block under Populating across multiple levels

Populate lets you get a list of a user's friends, but what if you also wanted a user's friends of friends? Specify the populate option to tell mongoose to populate the friends array of all the user's friends:

iweir
  • 21
  • 3