将来自 2 列的 json 数据与架构中的数据进行比较,并将最新数据覆盖到 mongoos 中的主要数据
Compare json data from 2 column with in schema and overwrite the latest data to main data in mongoos
我正在尝试从 json 数据构建一个页面,
“page_content”列将具有 json,而“override_content”列将具有 json 的一个或多个部分以及键。
“override_content”将始终具有最新更新,如果列为空则无覆盖数据。
预期结果是使用内部 json“id”比较“page_content”和“override_content”两列的数据,并将“page_content”中的内容替换为“[=” 36=]" 列
下面是例子
“page_content”中的数据如下所示,
组件有 3 child 每个 child 都有唯一的“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" 列有第一个和第三个 child 的最新数据,如下所示,
{
"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"
}
}
}
]
}
预期结果:
通过 child“id”比较两个列数据。
将“page_content”中的第一个和第三个 child 替换为“覆盖内容”
保留“page_content”中的第二个 Child
{
"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"
}
}
}
]
}
我的 API 在 ExpressJs 中如下所示,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);
});
}
});
谢谢
无法通过 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"}]}
我正在尝试从 json 数据构建一个页面, “page_content”列将具有 json,而“override_content”列将具有 json 的一个或多个部分以及键。 “override_content”将始终具有最新更新,如果列为空则无覆盖数据。 预期结果是使用内部 json“id”比较“page_content”和“override_content”两列的数据,并将“page_content”中的内容替换为“[=” 36=]" 列
下面是例子
“page_content”中的数据如下所示, 组件有 3 child 每个 child 都有唯一的“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" 列有第一个和第三个 child 的最新数据,如下所示,
{
"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"
}
}
}
]
}
预期结果: 通过 child“id”比较两个列数据。 将“page_content”中的第一个和第三个 child 替换为“覆盖内容” 保留“page_content”中的第二个 Child
{
"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"
}
}
}
]
}
我的 API 在 ExpressJs 中如下所示,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);
});
}
});
谢谢
无法通过 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"}]}