Search code examples
node.jsmongodbmongoosemongoose-schema

how Can I create parent comment which contain children comments in schema mongoose


The idea is about one comment contains children comments, it is about a comment contains responses. For that, I create my Comment schema like this:

import mongoose from 'mongoose';
//to connect to our database 
import db from '../db/connectDB.js'

const Schema = mongoose.Schema // Define Schema method= mongoose;

    const CommentSchema = new Schema({
    id_parent : {
        type: Number,
        required: true,
         },
    user_name: {
        type: String,
        },
    content: {
        type: String,
        },
    created_date: {
        type: Date,
        default: Date.now,
      },
    counter_like: {
        type: Number,
        required:false,
      },
    status : {
        type: String,
         },
    });
    export default mongoose.model('Comment', CommentSchema);

then my parentComment schema like this:

   import mongoose from 'mongoose';
    //to connect to our database 
    import db from '../db/connectDB.js'
    
    const SchemaParent = mongoose.Schema // Define Schema method= mongoose;
    
        const ParentCommentSchema = new Schema({
            parent_comment:{
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Comment'
            },    
            children_comments: [{
    
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'Comment'
                
            }]
           
        });
        export default mongoose.model('ParentComment', ParentCommentSchema);

It seems not clean, I don't want to insert the child comment every time I want to insert it directly in the the parent comment, I don't get the idea, any help


Solution

  • You could simply reference the parent ID in the comment itself

    const commentSchema = new Schema({
        _id,
        parentId : { type: mongoose.Schema.Types.ObjectId, ref: 'Comments' },
        user: { type: mongoose.Schema.Types.ObjectId, ref 'Users'},
        [...]
    });
    

    This way you can .find({parentId: commentId}) to find all the comments that would be "answers" of the commentId (parent). You will have an array of Comment

    Otherwise you can embed directly the comments inside each others.

    const commentSchema = new Schema({
        _id,
        user: { type: mongoose.Schema.Types.ObjectId, ref 'Users'}
    });
    
    commentSchema.add({comments: {type: [commentSchema], default: undefined})
    

    You'd have something like :

    comment {
        _id: 'qzrqrsrt',
        user: '1'
        content : "What do you guys think ?"
        comments : [
            {
                _id: 'qzrqzrqzr',
                user: '2',
                content: 'Ahah nice',
                comments: [
                    {
                        _id: 'qiorqzpçrq',
                        user: '1',
                        content: 'Thanks'
                    }
                ]
            },
            {
                _id: 'dryhsertse',
                user: '2'
                content: "I'll use it"
            }
        ]
    }
    

    But that would be a bit more difficult to find the comment you want to add answers to