2017-11-26 1 views
1

$lookupを扱うことは、同じコレクションを使用して結合を行うと思うまで楽しいものでした。

は、私は次のコレクションを持っていると言う:

{'_id': ObjectId('5a1a62026462db0032897179'), 
'department': ObjectId('5a1982646462db032d58c3f9'), 
'name': 'Standards and Quality Department', 
'type': 'sub'}, { 
'_id': ObjectId('5a1982646462db032d58c3f9'), 
'department': false, 
'desc': 'Operations Department', 
'type': 'main'} 

明らかなように、それは言う、最高レベルの部門を示すためにfalse可能性がありdepartmentキーを使用して、同じコレクション内がバックリンクです。私は結果を移入するために、次のクエリ(パイソン)を使用してい

{'__department': None, 
'_id': ObjectId('5a1982646462db032d58c3f9'), 
'department': false, 
'name': 'Operations Department', 
'type': 'main'}, 
{'__department': {'_id': ObjectId('5a1982646462db032d58c3f9'), 
        'department': 'false', 
        'name': 'Operations Department', 
        'type': 'main'}, 
'_id': ObjectId('5a1a62026462db0032897179'), 
'department': ObjectId('5a1982646462db032d58c3f9'), 
'name': 'Standards and Quality Department', 
'type': 'sub'} 

私が実際に取得していますが次のとおりです:

私を得ることを期待しています何

query = [{'$lookup': {'as': '__department', 
       'foreignField': '_id', 
       'from': 'departments', 
       'localField': 'department'}}, 
{'$unwind': '$__department'}, 
{'$group': {'__department': {'$first': '$__department'}, 
      '_id': '$_id', 
      'department': {'$first': '$department'}, 
      'name': {'$first': '$name'}, 
      'type': {'$first': '$type'}}}] 
for doc in conn.db.departments.aggregate(query): pprint(doc) 

{'__department': {'_id': ObjectId('5a1982646462db032d58c3f9'), 
        'department': 'false', 
        'name': 'Operations Department', 
        'type': 'main'}, 
'_id': ObjectId('5a1a62026462db0032897179'), 
'department': ObjectId('5a1982646462db032d58c3f9'), 
'name': 'Standards and Quality Department', 
'type': 'sub'} 

$unwindは、を適用する前に両方のドキュメントをまとめてグループ化している理由がわかりません私はそれらの両方を分離して取得します。

提案がありますか?

答えて

2

$lookupに一致するものが見つからなかった文書に空の配列__departmentを作成したためです。これは、あなたの孤児の文書がどのように見えるかです:

{ 
    "_id" : ObjectId("5a1982646462db032d58c3f9"), 
    "department" : false, 
    "desc" : "Operations Department", 
    "type" : "main", 
    "__department" : [] 
} 

あなたはこの文書の$unwindには何もありません巻き戻しされているので、それはプロセスで失われます。それを保持したい場合は、配列を「正規化」する必要があります。つまり、あなたの$lookup後、あなた$unwind前にこれを追加する必要があるだろう:

{ 
     $project: { 
      _id: 1, 
      department: 1, 
      name: 1, 
      type: 1, 
      __department: { 
       $cond: [{ 
         $eq: ["$__department", []] 
        }, 
        [{ 
         _id: 0, 
         department: "None", 
         desc: "None", 
         type: "None" 
        }], '$__department' 
       ] 
      } 
     } 
    } 

だから、すべて一緒に、それはそのようになります。

[{ 
     '$lookup': { 
      'as': '__department', 
      'foreignField': '_id', 
      'from': 'depart', 
      'localField': 'department' 
     } 
    }, 
    { 
     '$project': { 
      _id: 1, 
      department: 1, 
      name: 1, 
      type: 1, 
      __department: { 
       $cond: [{ 
         $eq: ["$__department", []] 
        }, 
        [{ 
         _id: 0, 
         department: "None", 
         desc: "None", 
         type: "None" 
        }], '$__department' 
       ] 
      } 
     } 
    }, 
    {'$unwind': "$__department"}, 
    {'$group': {'__department': {'$first': '$__department'}, 
      '_id': '$_id', 
      'department': {'$first': '$department'}, 
      'name': {'$first': '$name'}, 
      'type': {'$first': '$type'}}}] 
+0

おかげで、アレックス。これはまさに私が必要としていたものです! – ajawadmahmoud

関連する問題