猫鼬和NodeJS中的嵌套数组中的问题

时间:2020-08-07 19:41:41

标签: node.js post mongoose mongoose-schema

我有一个复杂的类别架构,其中包含一个子类别数组,每个子类别都有一个书籍数组。

有人可以帮助我对此类别架构进行适当的POST请求,在该类别中,我希望请求主体看起来像该架构包含一个嵌套在其中的类别,在每个子类别中嵌套的子类别数组,每个书籍类别中带有猫鼬 NodeJS 。这是模式:-

{
    "categoryName": "engineering",
    "subcategories": [
        {
            "subcategoryName": "chemical enginnering",
            "books": [
                {
                    "bookName": "",
                    "author": "",
                    "ratings": [3, 4, 5, 3],
                    "feedbcks": ["awesome book", "very amazing book"],
                    "price": 222
                }
            ]
        },
        {
            "subcategoryName": "mechanical engineering",
            "books": [
                {
                    "bookName": "",
                    "author": "",
                    "ratings": [3, 4, 5, 3],
                    "feedbcks": ["awesome book", "very amazing book"],
                    "price": 222
                }
            ]
        }
    ]
}

我尝试了此代码,但没有成功

const category = new Category({
    categoryName: req.body.categoryName,
    subcategories: [
      new SubCategory({
        subCategoryName: req.body.subCategoryName,
        books: [
          new Book({
            bookName: req.body.bookName,
            price: req.body.price,
            ratings: req.body.ratings,
            feedbacks: req.body.feedbacks,
            author: req.body.author
          })
        ]
      })
    ]
  });

  try {
    const savedCategory = await category.save();
    res.send(savedCategory);
  } catch (error) {
    res.send(error.message);
  }

1 个答案:

答案 0 :(得分:1)

我相信您的架构可能看起来像这样(subdocuments):

const Category = new Schema({
    categoryName: String,
    subcategories: [{
        subcategoryName: String,
        books: [{
            bookName: String,
            author: String,
            ratings: Array,
            feedbacks: Array,
            price: Number
        }]
    }]
})

const Book = new Schema({
    bookName: String,
    author: String,
    ratings: Array,
    feedbacks: Array,
    price: Number
})

const Subcategory = new Schema({
    subcategoryName: String,
    books: [Book]  
})

const Category = new Schema({
    categoryName: String,
    subcategories: [Subcategory]
})

要保存它,我想您可以将整个数据对象传递到Model中:

const category = {
    "categoryName": "engineering",
    "subcategories": [
        {
            "subcategoryName": "chemical enginnering",
            "books": [
                {
                    "bookName": "",
                    "author": "",
                    "ratings": [3, 4, 5, 3],
                    "feedbacks": ["awesome book", "very amazing book"],
                    "price": 222
                }
            ]
        },
        {
            "subcategoryName": "mechanical engineering",
            "books": [
                {
                    "bookName": "",
                    "author": "",
                    "ratings": [3, 4, 5, 3],
                    "feedbacks": ["awesome book", "very amazing book"],
                    "price": 222
                }
            ]
        }
    ]
}


const newCategory = new Category(category);
try {
    const savedCategory = await newCategory.save();
    res.send(savedCategory);
} catch (error) {
    res.send(error.message);
}

当然,这意味着您需要首先创建此类对象才能传递它,我以您的数据为例。您可以在前端或后端进行排序,对您而言更有意义。


或者,您可以先创建Category

const newCategory = new Category({ categoryName: req.body.categoryName })

然后添加剩余的元素(使用数组,您可以使用循环来动态push个元素):

const newCategory.subcategories = [{
    "subcategoryName": "chemical enginnering",
    "books": [
        {
            "bookName": "",
            "author": "",
            "ratings": [3, 4, 5, 3],
            "feedbacks": ["awesome book", "very amazing book"],
            "price": 222
        }
    ]
},
{
    "subcategoryName": "mechanical engineering",
    "books": [
        {
            "bookName": "",
            "author": "",
            "ratings": [3, 4, 5, 3],
            "feedbacks": ["awesome book", "very amazing book"],
            "price": 222
        }
    ]
}]

这实际上取决于您数据的结构以及如何从前端传递数据,但我想您已经明白了。


注意,示例数据中出现拼写错误,请将“ feedbcks”更改为“ feedbacks”。