2017-08-10 4 views
3

mongodb集約クエリに問題があります。 配列内のオプションフィールドのマップ

この

は、私は、これは以下の出力 -

{ 
"_id":"" 
"authors" : [ 
     { 
      "name" : "aaa", 
      "city" : "xyz", 
      "books":[[{"name":"blah"}], 
        [{"name":"blah blah"}]] 
     }, 
     { 
      "name" : "bbb", 
      "city" : "abc", 
      "books":[[{"name":"blah"}], 
        [{"name":"blah blah"}]] 
     }, 
     { 
      "name" : "ccc", 
      "city" : "xyz", 
      "books":[[{"name":"blah"}], 
        [{"name":"blah blah"}]] 
     }, ] 
} 

になり

db.book.aggregate([ { $project: {"id":1, "authors.name":1, "authors.city":1, 
      "authors.books": 
     { 
      $let :{ 
       vars:{"book":{$ifNull:["$authors.book", "absent"]}}, 
       in: { 
        $cond: {if: {$eq:["$$book","absent"]}, 
          then:[], 
          else:["$author.book"]} 
        } 
       } 
     } 
     } 
     },{ $out :"book" }]) 

をquery-これは非常に近い使い方

{ 
"_id":"" 
"authors" : [ 
     { 
      "name" : "aaa", 
      "city" : "xyz", 
      "book":{"name":"blah"} 
     }, 
     { 
      "name" : "bbb", 
      "city" : "abc", 
     }, 
     { 
      "name" : "ccc", 
      "city" : "xyz", 
      "book":{"name":"blah blah"} 
     }, ] 
} 

を使用していますDBスキーマです私が必要とする/欲しいもの -

{ 
"_id":"", 
"authors" : [ 
     { 
      "name" : "aaa", 
      "city" : "xyz", 
      "books":[ 
        {"name":"blah"}, 
        {"genre":"blah1"}] 
     }, 
     { 
      "name" : "bbb", 
      "city" : "abc", 
      "books":[] 
     }, 
     { 
      "name" : "ccc", 
      "city" : "xyz", 
      "books":[ 
        {"name":"blah blah"}, 
      {"genre":"blah1"}] 
     } 
    ] 
} 

問題は、ある著者の本の価値がすべての著者に割り当てられるということです。 1人の著者しかいない場合、コードは正常に動作しますが、1より大きい数があると失敗します。 値をジャンルに割り当てようとしていません。 MongoDBバージョン3.2.16を使用して、何が間違っているかを把握してくださいどのように修正することができますか?前もって感謝します。

答えて

0

$mapを使用して配列を反復処理し、bookフィールドに変更を適用します。また、あなたがタイプ間BSON比較作業として$let + $eq$gtとの表現に置き換える

db.book.aggregate([ 
    { 
    "$project": { 
     "id": 1, 
     "authors": { 
     "$map": { 
      "input": "$authors", 
      "as": "mauthor", 
      "in": { 
      "name": "$$mauthor.name", 
      "city": "$$mauthor.city", 
      "book": { 
       "$let": { 
       "vars": { 
        "lbook": { 
        "$ifNull": [ 
         "$$mauthor.book", 
         "absent" 
        ] 
        } 
       }, 
       "in": { 
        "$cond": { 
        "if": { 
         "$eq": [ 
         "$$lbook", 
         "absent" 
         ] 
        }, 
        "then": [], 
        "else": [ 
         "$$mauthor.book" 
        ] 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
]) 

よう

何か。

何か

よう
db.book.aggregate([ 
    { 
    "$project": { 
     "id": 1, 
     "authors": { 
     "$map": { 
      "input": "$authors", 
      "as": "mauthor", 
      "in": { 
      "name": "$$mauthor.name", 
      "city": "$$mauthor.city", 
      "book": { 
       "$cond": { 
       "if": { 
        "$gt": [ 
        "$$mauthor.book", 
        null 
        ] 
       }, 
       "then": [ 
        "$$mauthor.book" 
       ], 
       "else": [] 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
]) 
関連する問題