目前,我的資料庫中保存了 3 篇博文,我想根據其名稱檢索博文。
這是我在 MongoDB 中的資料庫:
[
{
_id: ObjectId("61de0e1562abb7ffd4089373"),
name: 'learn-react',
upvotes: 0,
comments: []
},
{
_id: ObjectId("61de0e1562abb7ffd4089374"),
name: 'learn-node',
upvotes: 0,
comments: []
},
{
_id: ObjectId("61de0e1562abb7ffd4089375"),
name: 'my-thoughts-on-resumes',
upvotes: 0,
comments: []
}
]
例如,如果我想發送一個 GET 請求以使用 url:localhost:8000/articles/learn-react 檢索“learn-react”,我將如何查詢資料庫?我只知道如何通過使用檢索集合中的所有檔案,db.find()但是我將如何通過 url 中的名稱檢索一篇特定的博客文章?
uj5u.com熱心網友回復:
使用$unwind你可以解構一個陣列欄位。然后使用$match您可以過濾檔案!試試這個代碼,它對你有幫助!
db.getCollection('your collection').aggregate([
{
$unwind: '$cource' //name of the array
},
{
$match: { 'cource.name': 'learn-react' }
},
]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/410187.html
標籤:
上一篇:如果$match條件總體上不滿足,如何更改此mongo查詢以回傳“00:00”?
下一篇:如何構建這些需求?(微服務)
