2017-02-10 44 views
2

我想要$unwind 2個字段,schoolhome。數據庫結構如同 ;

{ 
    "id" : 1, 
    "school" : [ 
     { 
      "path" : "school1", 
      "code" : "code1", 
     }, 
     { 
      "path" : "school2", 
      "code" : "code2", 
     }, 
     { 
      "path" : "school3", 
      "code" : "code3", 
     }, 
     { 
      "path" : "school4", 
      "code" : "code4", 
     } 
    ], 
    "home" : [ 
     { 
      "path" : "home1", 
      "code" : "homeCode1", 
     }, 
     { 
      "path" : "home2", 
      "code" : "homeCode2", 
     }, 
    ] 
} 

我想$unwindschoolhome領域,並得到他們每個人作爲;

{ 
    "id" : 1, 
    "school" : [ 
     { 
      "path" : "school1", 
      "code" : "code1", 
     } 
}, 
{ 
    "id" : 1, 
    "school" : [ 
     { 
      "path" : "school2", 
      "code" : "code2", 
     } 
}, 
{ 
    "id" : 1, 
    "school" : [ 
     { 
      "path" : "school3", 
      "code" : "code3", 
     } 
}, 
{ 
    "id" : 1, 
    "school" : [ 
     { 
      "path" : "school4", 
      "code" : "code4", 
     } 
}, 
{ 
    "id" : 1, 
    "home" : [ 
     { 
      "path" : "home1", 
      "code" : "homeCode1", 
     } 
}, 
{ 
    "id" : 1, 
    "home" : [ 
     { 
      "path" : "home2", 
      "code" : "homeCode2", 
     } 
} 

我寫的旨在獲得上述格式的查詢是;

db.collection.aggregate([ 
    {$unwind: "$school"}, 
    {$unwind: "$home"} 
]).pretty() 

query結果成對;

{ 
    "id" : 1, 
    "school" : { 
     "path" : "school1", 
     "code" : "code1" 
    }, 
    "home" : { 
     "path" : "home1", 
     "code" : "homeCode1" 
    } 
} 
{ 
    "id" : 1, 
    "school" : { 
     "path" : "school1", 
     "code" : "code1" 
    }, 
    "home" : { 
     "path" : "home2", 
     "code" : "homeCode2" 
    } 
} 
{ 
    "id" : 1, 
    "school" : { 
     "path" : "school2", 
     "code" : "code2" 
    }, 
    "home" : { 
     "path" : "home1", 
     "code" : "homeCode1" 
    } 
} 
{ 
    "id" : 1, 
    "school" : { 
     "path" : "school2", 
     "code" : "code2" 
    }, 
    "home" : { 
     "path" : "home2", 
     "code" : "homeCode2" 
    } 
} 
{ 
    "id" : 1, 
    "school" : { 
     "path" : "school3", 
     "code" : "code3" 
    }, 
    "home" : { 
     "path" : "home1", 
     "code" : "homeCode1" 
    } 
} 
{ 
    "id" : 1, 
    "school" : { 
     "path" : "school3", 
     "code" : "code3" 
    }, 
    "home" : { 
     "path" : "home2", 
     "code" : "homeCode2" 
    } 
} 
{ 
    "id" : 1, 
    "school" : { 
     "path" : "school4", 
     "code" : "code4" 
    }, 
    "home" : { 
     "path" : "home1", 
     "code" : "homeCode1" 
    } 
} 
{ 
    "id" : 1, 
    "school" : { 
     "path" : "school4", 
     "code" : "code4" 
    }, 
    "home" : { 
     "path" : "home2", 
     "code" : "homeCode2" 
    } 
} 

我怎樣才能$unwind 2場分開,而不是這導致對格式?

+0

爲什麼要投票?這不是一個很好解釋和直接的問題嗎?還是已經回答了,重複? – mmu36478

回答

0

這不是直接可能的。使用MongoDB 3.4有一個可能的解決方法。 $facet可用於在同一文檔的單個階段中執行多個管道操作集。它可以用於你的情況。

db.collection.aggregate({ 
    '$facet': { 
     'school': [{ 
      '$unwind': '$school' 
     }, { 
      '$project': { 
       _id: '1', 
       school: 1 
      } 
     }], 
     'home': [{ 
      '$unwind': '$home' 
     }, { 
      '$project': { 
       _id: '1', 
       home: 1 
      } 
     }] 
    } 
}, { 
    '$project': { 
     'schoolAndHome': { 
      '$setUnion': ['$school', '$home'] 
     } 
    } 
}, { 
    '$unwind': '$schoolAndHome' 
}, { 
    '$replaceRoot': { 
     'newRoot': '$schoolAndHome' 
    } 
})