這就是資料庫的樣子
[
//document1
{
"email": "[email protected]",
"cart": [
{
"id": 4,
"name": "Pen",
}
]
},
//document2
{
"email": "[email protected]",
"cart": [
{
"id": 42,
"name": "Ball",
}
]
}
]
如何撰寫貓鼬查詢以使用電子郵件在特定檔案中查找購物車內的專案。例如,我需要 id 為 4 的專案,該專案位于檔案的購物車中,電子郵件為“[email protected]”
- 查找帶有電子郵件“[email protected]”的檔案
- 在購物車陣列中的檔案中找到 id 為“4”的專案
uj5u.com熱心網友回復:
您可以嘗試運行此查詢
db.collection.find({
email: "[email protected]",
cart: {
$elemMatch: {
id: 4
}
}
})
我首先使用電子郵件地址進行了查詢。然后在陣列中我使用 elemMatch 來獲取與 id 匹配的 obj
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414127.html
標籤:
