【问题标题】:Mongoose reference collection or nested document 2 level deepMongoose 参考集合或嵌套文档 2 级深度
【发布时间】:2018-12-13 20:06:32
【问题描述】:

我在节点 js 上使用猫鼬,我有一个页面集合,例如

{
    "_id" : ObjectId("5b3cf0e7ee00450156711a47"),
    "language" : "en",
    "published" : true,
    "content" : [ 
        {
            "title" : "my title 1",
            "subTitle" : "subtitle 1",
            "items" : [ 
                {
                    "title" : "my item title 1",
                    "subTitle" : "item subtitle 1",

                }, 
                {
                    "title" : "my item title 2",
                    "subTitle" : "item subtitle 2",
                }
            ]
        }, 
        {
            "title" : "my title 2",
            "subTitle" : "subtitle 2",
        }
    ],
    "createdAt" : ISODate("2018-07-04T16:08:07.057Z"),
    "__v" : 0
}

“内容”数组包含许多对象,其中一些对象包含一个“项目”数组,其中也包含许多对象。 (嵌套在 2 层深)

我想知道引用内容对象而不是嵌套它们是否会更好,因为它们也有嵌套文档(在项目内部),所以它是 2 级深度嵌套。 我必须使用page.content.id(listId).items.id(id) 才能找到它们并使用page.content.id(listId).items.id(id)[key] = value; page.save(); 来更新它们。

考虑到最多不应有超过 3 个“项目”和 5 个“内容”,您认为最好的解决方案是更新嵌套在 2 级深层文档或具有参考/集合的内容?

我还计划进行会影响此决定的版本控制,因为更新可能/将创建文档的新版本。

【问题讨论】:

    标签: javascript mongodb mongoose


    【解决方案1】:

    我更喜欢这里的参考收藏

    页面架构

    {
    "language" : String,
    "published" : Boolean,
    "content" : [ 
       {type: mongoose.Schema.Types.ObjectId, ref: 'content'}
    ],
    "createdAt" : Date,
    }
    

    内容架构

    {
           "title" : String,
            "subTitle" : String,
            "items": [{type: mongoose.Schema.Types.ObjectId, ref: 'items'}]
    }
    

    项目架构

    {
       "title" :String,
       "subTitle" : String,
     }
    

    【讨论】:

    • 这个解决方案比嵌套解决方案提供什么?我的意思是它们都有效,但我只是想知道其中一个的原因。
    • 参考看起来更有条理。对于嵌套的,将来当您有很多内容和项目时会感到困惑。
    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多