文書のトップレベルに「タグ」フィールドを持たない以下のmongoコレクション「test」があります。
{
"_id" : 1,
"title" : "123 Department Report",
"year" : 2014,
"subsections" : [
{
"subtitle" : "Section 1: Overview",
"tags" : "SI",
"content" : "Section 1: This is the content of section 1."
},
{
"subtitle" : "Section 2: Analysis",
"tags" : "STLW",
"content" : "Section 2: This is the content of section 2."
},
{
"subtitle" : "Section 3: Budgeting",
"tags" : "TK",
"content" : {
"text" : "Section 3: This is the content of section3.",
"tags" : "HCS"
}
}
]
}
私の要件は、「タグ」値「STLW」のみを持つサブセクションを選択することです。 次の集計クエリを実行しています。
私は以下の出力では、すべてのサブ文書を取得していますクエリを実行しているのがdb.test.aggregate([
{ $redact: {
$cond: {
if: { $or: [ {$ifNull: ['$tags', true]}, {$eq: [ "$tags" , 'STLW' ]} ] },
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
]
:
{
"_id" : 1,
"title" : "123 Department Report",
"year" : 2014,
"subsections" : [
{
"subtitle" : "Section 1: Overview",
"tags" : "SI",
"content" : "Section 1: This is the content of section 1."
},
{
"subtitle" : "Section 2: Analysis",
"tags" : "STLW",
"content" : "Section 2: This is the content of section 2."
},
{
"subtitle" : "Section 3: Budgeting",
"tags" : "TK",
"content" : {
"text" : "Section 3: This is the content of section3.",
"tags" : "HCS"
}
}
]
}
しかし、私は以下の出力をしたいです。
{
"_id" : 1,
"title" : "123 Department Report",
"year" : 2014,
"subsections" :
{
"subtitle" : "Section 2: Analysis",
"tags" : "STLW",
"content" : "Section 2: This is the content of section 2."
}
}
これを達成するのに手伝ってもらえますか?
おかげ...........これはnecesarily redact
オペレータを必要としません
可能な複製(https://stackoverflow.com/questions [MongoDBを持つサブ文書の中で、配列をフィルタリングする方法]/15117030/how-to-filter-array-in-mongodb) – Veeram