1

我正在尝试从 json 数据构建一个页面,列“page_content”将包含 json,列“override_content”将包含一个或多个带键的 json 部分。“override_content” 将始终具有最新更新,如果列为空,则没有覆盖数据。预期结果是使用内部json“id”比较“page_content”和“override_content”列中的数据,并将“page_content”中的内容替换为“override_content”列

以下是示例

“page_content”中的数据如下所示,组件有 3 个孩子,每个孩子都有唯一的“id”

{
"components":[
    {
        "id":"uibuilder:fa10894e44b3460c93912198a99e3629",
        "component":{
            "name":"Hero",
            "properties":{
                "imageUrl":"https://images.unsplash.com/photo-1532298229144-0ec0c57515c7?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2000&q=80",
                "caption1":"New Collection",
                "caption2":"About this collection",
                "buttonText":"Click Me!"
            }
        },
        "componentStyle":{
            "large":{
                "display":"flex",
                "flexDirection":"column",
                "position":"relative",
                "flexShrink":"0",
                "boxSizing":"border-box",
                "marginTop":"20px",
                "minHeight":"20px",
                "minWidth":"20px",
                "overflow":"hidden"
            }
        }
    },
    {
        "id":"uibuilder:8818c12fe5c94b27b7f7033fec7910b6",
        "component":{
            "name":"Text",
            "properties":{
                "text":"<p>This iss text</p>"
            }
        },
        "componentStyle":{
            "large":{
                "display":"flex",
                "flexDirection":"column",
                "position":"relative",
                "flexShrink":"0",
                "boxSizing":"border-box",
                "marginTop":"20px",
                "minHeight":"20px",
                "minWidth":"20px",
                "overflow":"hidden"
            }
        }
    },
    {
        "id":"uibuilder:8818c12fe5c94b25c94b23fec033fec7",
        "component":{
            "name":"Text New",
            "properties":{
                "text":"<b>Component heading</b>"
            }
        },
        "componentStyle":{
            "large":{
                "display":"flex",
                "flexDirection":"column",
                "position":"relative",
                "flexShrink":"0",
                "boxSizing":"border-box",
                "marginTop":"10px",
                "minHeight":"10px",
                "minWidth":"10px",
                "overflow":"hidden"
            }
        }
    }
]

}

“override_content”列包含第一个和第三个孩子的最新数据,如下所示,

{
"components":[
    {
        "id":"uibuilder:fa10894e44b3460c93912198a99e3629",
        "component":{
            "name":"Hero",
            "properties":{
                "imageUrl":"https://images.unsplash.com/photo-1532298229144-0ec0c57515c7?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2000&q=80",
                "caption1":"Faucet Collection",
                "caption2":"",
                "buttonText":"Click Me!"
            }
        },
        "componentStyle":{
            "large":{
                "display":"flex",
                "flexDirection":"column",
                "position":"relative",
                "flexShrink":"0",
                "boxSizing":"border-box",
                "marginTop":"10px",
                "minHeight":"10px",
                "minWidth":"10px",
                "overflow":"hidden"
            }
        }
    },
    {
        "id":"uibuilder:8818c12fe5c94b25c94b23fec033fec7",
        "component":{
            "name":"Text New",
            "properties":{
                "imageUrl":"https://images.unsplash.com/photo-1532298229144-0ec0c57515c7?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2000&q=80",
                "caption1":"New Collection",
                "caption2":"About this collection",
                "buttonText":"Click Me!"
            }
        },
        "componentStyle":{
            "large":{
                "display":"flex",
                "flexDirection":"column",
                "position":"relative",
                "flexShrink":"0",
                "boxSizing":"border-box",
                "marginTop":"50px",
                "minHeight":"50px",
                "minWidth":"50px",
                "overflow":"hidden"
            }
        }
    }
]

}

预期结果:按子“id”比较两个列数据。将“page_content”中的第一个和第三个孩子替换为“覆盖内容”保留“page_content”中的第二个孩子原样

{
"components":[
    {
        "id":"uibuilder:fa10894e44b3460c93912198a99e3629",
        "component":{
            "name":"Hero",
            "properties":{
                "imageUrl":"https://images.unsplash.com/photo-1532298229144-0ec0c57515c7?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2000&q=80",
                "caption1":"Faucet Collection",
                "caption2":"",
                "buttonText":"Click Me!"
            }
        },
        "componentStyle":{
            "large":{
                "display":"flex",
                "flexDirection":"column",
                "position":"relative",
                "flexShrink":"0",
                "boxSizing":"border-box",
                "marginTop":"10px",
                "minHeight":"10px",
                "minWidth":"10px",
                "overflow":"hidden"
            }
        }
    },
    {
        "id":"uibuilder:8818c12fe5c94b27b7f7033fec7910b6",
        "component":{
            "name":"Text",
            "properties":{
                "text":"<p>This iss text</p>"
            }
        },
        "componentStyle":{
            "large":{
                "display":"flex",
                "flexDirection":"column",
                "position":"relative",
                "flexShrink":"0",
                "boxSizing":"border-box",
                "marginTop":"20px",
                "minHeight":"20px",
                "minWidth":"20px",
                "overflow":"hidden"
            }
        }
    },
    {
        "id":"uibuilder:8818c12fe5c94b25c94b23fec033fec7",
        "component":{
            "name":"Text New",
            "properties":{
                "imageUrl":"https://images.unsplash.com/photo-1532298229144-0ec0c57515c7?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2000&q=80",
                "caption1":"New Collection",
                "caption2":"About this collection",
                "buttonText":"Click Me!"
            }
        },
        "componentStyle":{
            "large":{
                "display":"flex",
                "flexDirection":"column",
                "position":"relative",
                "flexShrink":"0",
                "boxSizing":"border-box",
                "marginTop":"50px",
                "minHeight":"50px",
                "minWidth":"50px",
                "overflow":"hidden"
            }
        }
    }
]

}

我在 ExpressJs 中的 API 如下所示,Pages 是我尝试使用 mongoose 聚合的集合

router.get('/pages/findit/:_id', (req: Request, res: Response) => {
  const id = req.params._id;
  if (id && id.length > 0) {
    const pid = mongoose.Types.ObjectId(id);
    Pages.aggregate([
      {
        $match: { _id: pid }
      }
    ]).exec((err, data) => {
      if (err) { res.status(HttpStatusEnum.badserver).json(err); return; };
      let result: IResponse = {
        data: data
      };
      res.status(HttpStatusEnum.success).json(result);
    });
  }
});

谢谢

4

1 回答 1

0

无法通过 Mongoose 聚合找到方法,但使用外部方法比较和合并 json 数组,一旦我们从集合中获得结果,就会将数据传递到下面的脚本中进行比较和合并,(在我的情况下合并操作需要一次录制一个)

const sourceJson = { components: [{ id: 'a' }, { id: 'b', test: 'a' }] };
const customJson = { components: [{ id: 'b', test: 'b' }, { id: 'c' }] };
      const result = sourceJson.components.map(sj => ({
        ...sj, 
        ...customJson.components.find(cj => sj.id === cj.id)
      }))
      return result;

输出:

{"components":[{"id":"a"},{"id":"b","test":"b"},{"id":"c"}]}
于 2021-07-01T12:06:09.733 回答