我正在從陣列中查找資料。那是一門課程,我想通過標題找到一篇文章,標題存盤在課程的文章中。我一步一步來......(1)我想按名稱精修課程,(2)在它之后,我想通過使用文章的標題(3)找到一篇文章到課程的文章中并渲染文章和課程到網頁這里是代碼
const course = req.params.course
const article_title = req.query.article
Course.findOne({name: course , article:article_title}).then(function(data){
console.log(data)
res.render('article',{data :data})
}).catch((err)=>{console.log(err)});
這是資料庫
_id:61c057cfd70f2fb178d4e996
name:"Soft "
discription:"House"
article:Array
0:Object
title:"Where does it come from?"
content:"<p>Title is very important for an article to explain content | article..."
_id:61c05d4a3905f61f72a8e61b
1 :Object
2:Object
uj5u.com熱心網友回復:
要搜索,您可以使用:
Couse.findOne({name: course, 'article.title': article_title})
因此,您將獲得包含所需文章的檔案。
使用投影 and $elemMatch,您可以過濾文章陣列本身,以便只有您想要的子檔案在那里:
Couse.findOne({name: course, 'article.title': article_title}, {
article: {
$elemMatch: {
title: article_title
}
}
})
你可以在 MongoPlayground https://mongoplayground.net/p/SVXNGE6tuW7試試
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/389746.html
