【问题标题】:I want to append the child categories to parent categories as a json object after using find query使用查找查询后,我想将子类别作为 json 对象附加到父类别
【发布时间】:2019-10-27 04:56:00
【问题描述】:
I have parent categories and child categories in my database as shown            below.
[{
"name": "electronics"
  , "parent": "/"
  , "category": "/electronics"
}, {
"name": "embedded"
  , "parent": "/electronics"
  , "category": "/electronics/embedded"
},
{
"name": "laptops"
  , "parent": "/electronics"
  , "category": "/electronics/laptops"
},
{
    "name": "dell"
  , "parent": "/electronics/laptops"
  , "category": "/electronics/laptops/dell"
},
{
    "name": "acer"
  , "parent": "/electronics/laptops"
  , "category": "/electronics/laptops/acer"

},

 {
    "name": "cases"
  , "parent": "/"
  , "category": "/cases"
}, {
"name": "big"
  , "parent": "/cases"
  , "category": "/cases/big"
}, {
"name": "small"
  , "parent": "/cases"
  , "category": "/cases/small"
}]

I want those category to be fetched by appending the child category       with the parent category as json object . ie
[electronics:{
  name:embedded
},
{
name:laptops{
name:dell
},{
name:acer
},
},
cases:{
name:big
},{
name:small
}
]

我已使用正则表达式获取数据,但这些数据未按预期出现。

我希望这些类别出现在嵌套的 json 对象中,如上所示。子类别应附加到相应的父类别。

【问题讨论】:

    标签: node.js regex mongodb express mongoose


    【解决方案1】:

    我的解决方案:

    let newCategory={name:'abc'};
    let data = listCategory;
    listCategory.push(newCategory);
    listCategory.save();
    

    【讨论】:

    • 您能详细说明一下吗?
    • 你能告诉我你的fecth data code吗?我会帮你详细的。
    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2022-01-05
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    相关资源
    最近更新 更多