我有一個名爲Org
集合包含資源(Resources
架構),這是團隊之間共享(Teams
模式)如何簡化mongodb集合?
{
"_id": ObjectId("511cfbc9d593e5290c000005"),
"name": "Some org name",
"resources": [
{
"_id": ObjectId("511cfbc9d593e5290c000007"),
"name": "Printer1",
/* mongoose.Schema.Types.Mixed */
"details": {
"ip": "192.168.1.99"
}
}, {
"_id": ObjectId("511cfbc9d593e5290c000008")
"name": "Fax1",
"details": {
"number": "XXXXXXXXXXXX"
}
}
],
"teams" : [
{
"_id": ObjectId("511cfbc9d593e5290c000012"),
"name": "sales",
/*"resources": {type: [mongoose.Schema.Types.ObjectId], ref: 'Resources'}*/
"resources": [ObjectId("511cfbc9d593e5290c000007")]
}, {
"_id": ObjectId("511cfbc9d593e5290c000006"),
"name": "developer",
"resources": [ObjectId("511cfbc9d593e5290c000007"), ObjectId("511cfbc9d593e5290c000008")]
}
]
}
也有一個People
集合,這是一個團隊的一部分。
{
"name": "Peter",
"designation": "senior s/w engg.",
"contact": {}
/*"teams": {type: [mongoose.Schema.Types.ObjectId], ref: 'Teams'}*/
"teams": [ObjectId("511cfbc9d593e5290c000006")]
}
現在我想跳過多個更新,如果資源或團隊被更改,所以我沒有使用嵌套文件。我無法從Teams
架構中獲得ref
Resources
架構。因此,爲了得到以下結果,我必須經歷非常複雜的聚合函數。
{
"name": "Peter",
"designation": "senior s/w engg.",
"contact": {}
"teams": [{
"_id": ObjectId("511cfbc9d593e5290c000006"),
"name": "developer",
"resources": [{
"_id": ObjectId("511cfbc9d593e5290c000007"),
"name": "Printer1",
"details": {
"ip": "192.168.1.99"
}
}, {
"_id": ObjectId("511cfbc9d593e5290c000008")
"name": "Fax1",
"details": {
"number": "XXXXXXXXXXXX"
}
}]
}]
}
可以請一些建議,如果我在db設計中做錯了什麼。有沒有更簡單的方法?