我有以下文件:檢索MongoDB中集合的對象陣列中的特定元件
{
"_id" : 257655,
"name" : "Arrow",
"airsDayOfWeek" : "Wednesday",
"airsTime" : "8:00 PM",
"firstAired" : ISODate("2012-10-10T00:00:00.000+0000"),
"runtime" : 45,
"imdbId" : "tt2193021",
"seasons" : [
{
"number" : 1,
"episodes" : [
{
"_id" : 4599381,
"number" : 1,
"overview" : "Diggle and Felicity ...",
},
{
"_id" : 4365918,
"number" : 2,
"overview" : "Oliver heads ...",
}
]
},
{
"number" : 2,
"episodes" : [
{
"_id" : 4599398,
"number" : 1,
"overview" : "With crime in Starling Cit ...",
},
{
"_id" : 4365989,
"number" : 2,
"overview" : "Oliver finds out the medicine ...",
}
]
}
]
}
以下查詢
db.shows.find({ 'seasons.episodes.number': 1, 'seasons.number': 1, _id: 257655 })
返回與seasons.episodes所有記錄文件
我也試過這個
db.shows.aggregate([
{$match: { '$and': [ {'seasons.number': 1}, { 'seasons.episodes.number': 1}, {_id: 257655}] }},
{$project: {
seasons: {
$filter: {
input: '$seasons',
as: 'seasons',
cond: {$eq: ['$$seasons.episodes.number', 1]}
}
}
}}
])
但我得到了一個錯誤:異常:無效運算符「$過濾器」
不過,我想只能用含有seasons.episodes.number = 1和seasons.number = 1
陣列獲取文檔{
seasons" : [
{
"number" : 1,
"episodes" :
{
"_id" : 4599381,
"number" : 1,
"overview" : "Diggle and Felicity ...",
}
}
]
}
我該怎麼做?
[只檢索MongoDB集合中對象數組中的查詢元素]的可能重複(http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array -in-mongodb-collection) – styvane