简体   繁体   中英

Mongoose reference collection or nested document 2 level deep

I'm using mongoose on node js and I'm having a Page collection such as

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

The "content" array includes many objects and some of those object include an "items" array that includes many object too. (nested on 2 level deep)

I was wondering if it wouldn't be better to reference content object instead of having them nested, because they're having nested documents too (inside items) so it's a 2 level deep nesting. I have to use page.content.id(listId).items.id(id) in order to find them and page.content.id(listId).items.id(id)[key] = value; page.save(); page.content.id(listId).items.id(id)[key] = value; page.save(); to update them.

Considering there shouldn't have more than 3 "items" and 5 "content" maximum, what do you think would be the best solution, updating nested on 2 level deep document or content having a reference/collection?

I also plan to do a versioning which would influence this decision, as updating could/would create a new version of the document.

I would prefer reference collection here

Page Schema

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

Content Schema

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

Items Schema

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM