hi there I am not getting data from a DB object which is storing the id of another DBS, actually, this is a blog website so I am posting comments and acting comments but there are problems I am getting the same comments on all posts but I want, every post should have their own comment. here is post schema
const blogSchema = new mongoose.Schema({
title: String ,
content: String,
image:{data: Buffer,contentType: String},
comment:[
{
type:mongoose.Schema.Types.ObjectId, ref:'Comment'
}
]
});
here is the comment schema
var commentSchema = new mongoose.Schema({
name:{
type:String,
required: "this field is required"
},
comment:{
type:String,
required:"this filed is required"
},
blog:{
type:mongoose.Schema.Types.ObjectId,
ref: 'Blog'
}
},{
timestamps: true
})
node js router which is getting post but not comment
pp.get("/post/:postname",(req,res)=>{
// const requesttitle = _.lowerCase(req.params.postname);
const requesttitle = req.params.postname;
Blog.findOne({_id: requesttitle} ,(err ,got)=>{
if(err){
console.log(err)
}else{
const data= got.comment.find({})
console.log(data)
res.render('post',{post:got });
}
})
})
I think the problem lies in your schematic. In your blogSchema have references to many Comment documents, and in your commentSchema have a reference to a single "Blog" ( I suggest not calling it a "blog" but rather a "post" since that is what it is ). This duplicate reference is not necessary in most cases.
Since in your setup, a single comment can only be a child of a specific post, this would be the reference you'd choose. The post document itself does not need to know directly which comments are included, as that information is already in the comments document.
For your post I would suggest the following schematic:
Post Schema const postSchema = new mongoose.Schema({ title: String , content: String, image: { data: Buffer, contentType: String } });For your comment , I would suggest the following schematic:
// Comment Schema const commentSchema = new mongoose.Schema({ name: { type: String, required: "this field is required" }, comment: { type: String, required: "this filed is required" }, post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' } });Now sure the next step depends on your entire front end being set up, but having schematics like this would allow you to do something like:
pp.get("/post/:id", async (req,res) => { const id = req.params.id; const post = await Post.findOne({ _id: id }); const comments = await Comment.find({ post: id }); res.render('post', { post: post, comments: comments }); });the one-way relationship means less work if a comment is created or deleted.
ability to get comments and/or posts or both in one api call.
Requires 2 database calls if posts and comments are requested.
As an alternative to using Referenced Documents, you can use Subdocuments .
For your commentSchema , that means you won't need to create a separate Model from it. However, your postSchema should look like this:
const commentSchema = new mongoose.Schema({ message : { type : String } }); const postSchema = new mongoose.Schema({ comments : [commentSchema] });👆 This would by default include all comments on the post if you retrieve the post from the database. However, it would also require different code to interact with those comments (add, remove, ...) but you can read about it in the docs I'm sure.