将 2 列中的 json 数据与模式中的数据进行比较,并将最新数据覆盖为 mongoos 中的主要数据

Posted

技术标签:

【中文标题】将 2 列中的 json 数据与模式中的数据进行比较,并将最新数据覆盖为 mongoos 中的主要数据【英文标题】:Compare json data from 2 column with in schema and overwrite the latest data to main data in mongoos 【发布时间】:2021-09-12 06:23:59 【问题描述】:

我正在尝试从 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 是集合 我正在尝试使用猫鼬聚合

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);
    );
  
);

谢谢

【问题讨论】:

【参考方案1】:

无法通过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"]

【讨论】:

以上是关于将 2 列中的 json 数据与模式中的数据进行比较,并将最新数据覆盖为 mongoos 中的主要数据的主要内容,如果未能解决你的问题,请参考以下文章

从 pandas DataFrame 中的列中提取 JSON 数据

属性表模式与将所有属性存储在 json 列中 [重复]

将数据框列中的日期与单个日期进行比较

如何将 pandas 列中的 JSON 数据转换为新列

将 Google BigQuery 中一个表中的 XML 数据转换为同一表中另一列中的 JSON 数据

根据 SQL Server 2008 R2 中特定列中的模式更改对行进行分组