根据祖先列表和深度值构建嵌套数组

时间:2020-09-08 02:39:03

标签: javascript node.js mongodb mongoose

我有引用自己的类别模型。以下是mongodb中的类别数据。

{   "_id":{"$oid":"5f55acc029d19e1ac402908f"},
    "parents":null,
    "name":"pizza",
    "slug":"pizza",
    "userID":"5f38c867b10f740e38b12198",
    "ancestors":[],
}
{   "_id":{"$oid":"5f55b3c0a7b68b3bc0fe16c5"},
    "parents":{"$oid":"5f55acc029d19e1ac402908f"},
    "name":"premium",
    "slug":"premium",
    "userID":"5f38c867b10f740e38b12198",
    "ancestors":[{
                    "_id":{"$oid":"5f55acc029d19e1ac402908f"},
                    "name":"pizza",
                    "parents":null,
                    "slug":"pizza",
                    "depth":{"$numberInt":"0"}
                }],
}
{   "_id":{"$oid":"5f55b726b6b12042d09057c2"},
    "parents":{"$oid":"5f55b3c0a7b68b3bc0fe16c5"},
    "name":"peri peri chicken",
    "slug":"peri-peri-chicken",
    "userID":"5f38c867b10f740e38b12198",
    "ancestors":[{
                    "_id":{"$oid":"5f55b3c0a7b68b3bc0fe16c5"},
                    "name":"premium",
                    "parents":"5f55acc029d19e1ac402908f",
                    "slug":"premium",
                    "depth":{"$numberInt":"1"}
                },
                {   
                    "_id":{"$oid":"5f55acc029d19e1ac402908f"},
                    "parents":null,
                    "name":"pizza",
                    "depth":{"$numberInt":"0"},
                    "slug":"pizza"
                }]
}
{   "_id":{"$oid":"5f55bb8be3088f473c4e15ac"},
    "parents":null,
    "name":"burger",
    "slug":"burger",
    "userID":"5f38c867b10f740e38b12198",
    "ancestors":[]
}

我的猫鼬模型如下

const ItemCategorySchema = new Schema({
    name: {
        type: String,
        required: true
    },
    slug: {
        type: String,
        index: true
    },
    parents: {
        type: Schema.Types.ObjectId,
        default: null,
        ref: 'ItemCategory'
    },
    ancestors: [{
        _id: {
            type: Schema.Types.ObjectId,
            ref: "ItemCategory",
            index: true
        },
        name: { type: String },
        parents: { type: String },
        slug: { type: String },
        depth: { type: Number }     
    }],
    userID: {
        type: String,
        required: true
    }
});

如何使用祖先和深度信息构建如下所示的数组。我在用猫鼬。猫鼬中是否有任何功能可以将所有自参照类别填充到任意数量的级别或深度?

const options = [
    { value: 'pizza', label: 'pizza',
      options: [
        { value: 'premium', label: 'premium',
          options: [
            { value: 'peri-peri-chicken', label: 'peri peri chicken' },
            { value: 'chicken-and-bacon', label: 'chicken and bacon'},
          ],
        },
        { value: 'traditional', label: 'traditional',
          options: [
            { value: 'beef-and-onion', label: 'beef and onion' },
          ],
        },
      ],
    },
    { value: 'burger', label: 'burger',
      options: [
        { value: 'chicken', label: 'chicken' },
        { value: 'beef', label: 'beef' },
      ],
    },
]

0 个答案:

没有答案