gpt4 book ai didi

javascript - 如何简化mongodb集合?

转载 作者:行者123 更新时间:2023-11-30 13:08:03 25 4
gpt4 key购买 nike

我有一个名为 Org 的集合,其中包含在团队(Teams 架构)之间共享的资源(Resources 架构)

{
"_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"
}
}]
}]
}

有人可以建议我是否在数据库设计中做错了什么吗?有没有更简单的方法?

最佳答案

Now I want of skip multiple updates if a resource or team is changed, so I did not used nested documents.

您会发现这是在 MongoDB 模式设计中需要考虑的常见权衡。实际上,这将归结为跳过多个更新对您来说有多重要。多次更新通常并不像您想象的那样具有挑战性,事实证明这是正确的决定,尤其是在需要读取简单性和性能时。

Can some please suggest whether if I am doing something wrong in the db design. Is there any simpler way?

考虑到您声明的避免多次更新的意图,我认为您的方法没有问题。不过,就我个人而言,在这种情况下,我会采用嵌套文档方法并尝试解决多个更新问题。这将意味着更简单的查询,以及更好的性能(这往往是应用程序开发中最重要的考虑因素)。

关于javascript - 如何简化mongodb集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14939913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com