考慮一下:從JSON對象的數組MongoDB中
{
"movies": [
{
"title": "Star Wars",
"year": 1977,
"director": "George Lucas"
},
{
"title": "The Empire Strikes Back",
"year": 1980,
"director": "Irvin Kershner"
},
{
"title": "Return of the Jedi",
"year": 1983,
"director": "Richard Marquand"
},
{
"title": "The Phantom Menace",
"year": 1999,
"director": "George Lucas"
},
{
"title": "Attack of the Clones",
"year": 2002,
"director": "George Lucas"
},
{
"title": "Revenge of the Sith",
"year": 2005,
"director": "George Lucas"
},
{
"title": "The Force Awakens",
"year": 2015,
"director": "J.J. Abrams"
}
]
}
我試圖拔出由喬治·盧卡斯執導的所有電影。這是我曾嘗試和返回的所有項目:
db.movies.find({"movies.director" : "George Lucas"}).pretty()
而且這導致一個錯誤:
db.movies.find({"$pull" : {"movies.director" : "George Lucas"}}).pretty()
請讓我知道如何查詢數據庫,檢索只有電影,其中導演鑰匙有「喬治盧卡斯」作爲價值。
使用[$過濾器(https://docs.mongodb.com/manual/reference/operator/aggregation/filter/) – Veeram
你怎麼能做到這一點? – JohnSnow
您需要使用$ unwind聚合函數 – akinjide