'how to query inside subdocument in mongoose
I have blogs nested in User
const blogSchema = new mongoose.Schema({
title: String,
body: String,
comments: [{ body: String, date: Date }],
image:{type:String,required:true},
date: { type: Date, default: Date.now }})
const userSchema = new mongoose.Schema({
name:String,
email:String,
password:String,
blogs:[blogSchema]
})
const User = mongoose.model('User', userSchema);
app.get("/blog",(req,res)=>{
const {id,user} = req.query
User.findOne({_id:user},(err,results)=>{
if(!err){
console.log(results.blogs)
}else{
console.log(err)
}
})
})
I have User._id as user and blogs._id as id I am getting all the blogs but I want to get a single blog form array of objects by id
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
