2
我想要$unwind
2個字段,school
和home
。數據庫結構如同 ;
{
"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",
},
]
}
我想$unwind
school
和home
領域,並得到他們每個人作爲;
{
"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場分開,而不是這導致對格式?
爲什麼要投票?這不是一個很好解釋和直接的問題嗎?還是已經回答了,重複? – mmu36478