如何使用无限嵌套子级填充文档?

ogsagwnx  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(206)

我有一个带有childs引用的树结构,我想按id查找一个文档,并用无限嵌套的子级填充它。
我想要这个结果:

[
      {
        _id: '60e884adb73aa620d0583e602',
        code: 'code1',
        value: 'value_1002',
        label: 'dechet_1002',
        childrens: [
          {
            _id: '60e88489b73aa620d0583e5f3',
            code: 'code1',
            value: 'value_1001',
            label: 'dechet_1001',
            childrens: [
              {
                _id: '60e88444b73aa620d0583e5e5',
                code: 'code1',
                value: 'value_1000',
                label: 'dechet_1000',
                childrens: []
              }
            ]
          }
        ]
      }
    ]

我尝试了预挂钩或中间件功能,但它对我不起作用:

// In the Model
    const mySchema = new mongoose.Schema(
      {
        label: {
          type: String,
          required: true
        },
        //...
        childrens: [
          {
            _id: {
              type: ObjectId,
              ref: 'patterns'
            },
            label: String,
            //...
          }
        ]
      },
      { timestamps: true }
    );
   function autoPopulateChildrens(next) {
     this.populate('childrens');
     next();
   }

     mySchema
       .pre('findOne', autoPopulateChildrens)
       .pre('find', autoPopulateChildrens)
       .pre('findById', autoPopulateChildrens);
    const myModel = mongoose.model('patterns', mySchema);
    module.exports = myModel;

            // In the Controller
                await myModel
                          .findOne({_id: pId})
                          .select('_id label childrens')
                          .populate('childrens');

findbyid和findone不适合我。我还尝试将我的 Mongoose 更新到最新版本。我使用的是“ Mongoose ”:“^5.9.27”。
我尝试使用此功能手动执行此操作:

const getDescendants = async (pId) => {
  let descendants = [];
  let array = [];
  let item = await patternModel.findOne({ _id: pId });
  let { _id, code, value, label, childrens } = item;
  array.push({ _id, code, value, label, childrens });

  while (array.length > 0) {
    let currentnode = array.pop();
    let children = await patternModel.find({
      _id: { $in: currentnode.childrens }
    });

    children.forEach((child) => {
      let { _id, code, value, label, childrens } = child;
      descendants.push({ _id, code, value, label, childrens });
      if (child.childrens.length > 0) {
        array.push(child);
      }
    });
  }
  return descendants;
};

结果是:

[
         {
            "_id":"60e884adb73aa620d0583e60",
            "code":"code1",
            "value":"value_1002",
            "label":"dechet_1002",
            "childrens":[
               {
                  "_id":"60e88489b73aa620d0583e5f",
                  "code":"code1",
                  "value":"value_1001",
                  "label":"dechet_1001"
               }
            ]
         },
         {
            "_id":"60e88489b73aa620d0583e5f",
            "code":"code1",
            "value":"value_1001",
            "label":"dechet_1001",
            "childrens":[
               {
                  "_id":"60e88444b73aa620d0583e5e",
                  "code":"code1",
                  "value":"value_1000",
                  "label":"dechet_1000"
               }
            ]
         },
         {
            "_id":"60e88444b73aa620d0583e5e",
            "code":"code1",
            "value":"value_1000",
            "label":"dechet_1000",
            "childrens":[]
         }
      ]

我想要的是:

[
      {
        _id: '60e884adb73aa620d0583e602',
        code: 'code1',
        value: 'value_1002',
        label: 'dechet_1002',
        childrens: [
          {
            _id: '60e88489b73aa620d0583e5f3',
            code: 'code1',
            value: 'value_1001',
            label: 'dechet_1001',
            childrens: [
              {
                _id: '60e88444b73aa620d0583e5e5',
                code: 'code1',
                value: 'value_1000',
                label: 'dechet_1000',
                childrens: []
              }
            ]
          }
        ]
      }
    ]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题