0

I have a document called MentorProfile that has a reference to document CommentFeed. I am executing the following:

MentorProfile.findOneAndUpdate(
        {
            _id: req.body.profileId,
            'lessons._id': req.body.lessonId,
            'lessons.completedList.userData': { $ne: req.user._id }
        },
        {
            $push: {
                'lessons.$.completedList': {
                    name: req.user.name,
                    avatar: req.body.userAvatar,
                    userRecord: req.user._id
                }
            },
            $push: {
                'lessons.$.commentFeed.comments': {
                    user:{name:'name', avatar:'avatar', userRecord:'8u38u3jfj'}
                    text: 'test'
                }
            }
        },
        { new: true }
    )

I am wanting to push a new comment to the comments array, where the array is nested as follows:

MentorProfile>CommentFeed>comments

The code snippet I attached doesn't work, i'm sure i'm missing something obvious

MentorProfile snippet of relevant info

lessons: [
        {
            completedList: [
                {
                    name: { type: String, required: true },
                    avatar: { type: String, required: true },
                    userData: {
                        type: Schema.Types.ObjectId,
                        ref: 'User'
                    }
                }
            ],
            stepType: { type: String, required: true },
            commentFeed: { type: Schema.Types.ObjectId, ref: 'CommentFeed', required: true }
        }
    ]

CommentFeed snippet

comments: [
        {
            user: {
                name: { type: String, required: true },
                avatar: { type: String, required: true },
                userRecord: {
                    type: Schema.Types.ObjectId,
                    ref: 'User'
                }
            },
            text: { type: String, required: true }]}
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Snoopy
  • 1,257
  • 2
  • 19
  • 32
  • `commentFeed` is a reference to a foreign collection. You don't add items to this collection, but rather **insert** new documents into the foreign collection. The only thing you would do here is `$push` new items into `completedList` for the matching `lessons` entry ( via the `lessons._id` predicate ) or `$set` the `commentFeed` to it's foreign reference. Though the latter cannot be done without the process shown in [Updating a Nested Array with MongoDB](https://stackoverflow.com/a/23577266/2313887) using the filtered positional `$[]` operator. – Neil Lunn Mar 27 '19 at 09:00
  • Here you are pushing complete object instead you should push objectID in `commentFeed` field in `MentorProfile` collection. And `commentFeed` document should be inserted in `CommentFeed` collection and after inserting you should update `MentorProfile` collection by pushing `commentFeed_id` to the field `commentFeed`. – Vikash_Singh Mar 27 '19 at 09:05

0 Answers0