为了账号安全,请及时绑定邮箱和手机立即绑定

我如何为此设计架构?

我如何为此设计架构?

慕丝7291255 2022-01-07 21:39:20
我正在开发一个 MERN 应用程序。我需要为相当简单的移动商店网站设计架构。数据应采用以下格式    {  [    "IOS":[      "Apple":[        {          "model":"Iphone6"        },        {          "model":"Iphone7"        }      ]    ],    "Android":[      "Samsung":[        {          "model":"S6"        },        {          "model":"S7"        }      ],      "OnePlus":[        {          "model":"oneplu6"        },        {          "model":"onplus7"        }      ]    ],    "Windows":[      "Nokia":[        {          "model":"Nokia 7.2"        }      ]    ]  ]}我如何在 mongo/mongoose 中为此设计模式?
查看完整描述

1 回答

?
森林海

TA贡献2011条经验 获得超2个赞

如果不需要使用操作系统和品牌名称作为键,我有这样的解决方案。


我会像这样设置我的模式:


const mongoose = require("mongoose");


const operatingSystemSchema = new mongoose.Schema({

  name: {

    type: String,

    required: true

  }

});


const brandSchema = new mongoose.Schema({

  name: {

    type: String,

    required: true

  }

});


const productSchema = new mongoose.Schema({

  model: {

    type: String,

    required: true

  },

  operatingSystem: {

    type: mongoose.Schema.Types.ObjectId,

    ref: "OperatingSystem"

  },

  brand: {

    type: mongoose.Schema.Types.ObjectId,

    ref: "Brand"

  }

});


module.exports = {

  OperatingSystem: mongoose.model("OperatingSystem", operatingSystemSchema),

  Brand: mongoose.model("Brand", brandSchema),

  Product: mongoose.model("Product", productSchema)

};

根据模式插入文档后,我们可以使用以下聚合按操作系统和品牌进行分组:


db.products.aggregate([

  {

    $lookup: {

      from: "operatingsystems",

      localField: "operatingSystem",

      foreignField: "_id",

      as: "operatingSystems"

    }

  },

  {

    $lookup: {

      from: "brands",

      localField: "brand",

      foreignField: "_id",

      as: "brands"

    }

  },

  {

    $unwind: "$operatingSystems"

  },

  {

    $unwind: "$brands"

  },

  {

    $replaceRoot: {

      newRoot: {

        $mergeObjects: [

          "$$ROOT",

          {

            operatingSystem: "$operatingSystems.name",

            brand: "$brands.name"

          }

        ]

      }

    }

  },

  {

    $project: {

      brands: 0,

      operatingSystems: 0

    }

  },

  {

    $group: {

      _id: {

        "operatingSystem": "$operatingSystem",

        "brand": "$brand",


      },

      products: {

        $push: "$$ROOT"

      }

    }

  },

  {

    $group: {

      "_id": "$_id.operatingSystem",

      "data": {

        "$push": {

          "brand": "$_id.brand",

          "models": "$products.model"

        }

      }

    }

  },

  {

    $project: {

      "OS": "$_id",

      "_id": 0,

      "data": 1

    }

  }

])

操场


以及快递方面的示例路线:


router.get("/", async (req, res) => {

  const result = await Product.aggregate([

    {

      $lookup: {

        from: "operatingsystems",

        localField: "operatingSystem",

        foreignField: "_id",

        as: "operatingSystems"

      }

    },

    {

      $lookup: {

        from: "brands",

        localField: "brand",

        foreignField: "_id",

        as: "brands"

      }

    },

    {

      $unwind: "$operatingSystems"

    },

    {

      $unwind: "$brands"

    },

    {

      $replaceRoot: {

        newRoot: {

          $mergeObjects: [

            "$$ROOT",

            {

              operatingSystem: "$operatingSystems.name",

              brand: "$brands.name"

            }

          ]

        }

      }

    },

    {

      $project: {

        brands: 0,

        operatingSystems: 0

      }

    },

    {

      $group: {

        _id: {

          operatingSystem: "$operatingSystem",

          brand: "$brand"

        },

        products: {

          $push: "$$ROOT"

        }

      }

    },

    {

      $group: {

        _id: "$_id.operatingSystem",

        data: {

          $push: {

            brand: "$_id.brand",

            models: "$products.model"

          }

        }

      }

    },

    {

      $project: {

        OS: "$_id",

        _id: 0,

        data: 1

      }

    }

  ]);


  res.send(result);

});

结果将如下所示:


[

  {

    "OS": "Windows",

    "data": [

      {

        "brand": "Nokia",

        "models": [

          "Nokia 7.2"

        ]

      }

    ]

  },

  {

    "OS": "Android",

    "data": [

      {

        "brand": "OnePlus",

        "models": [

          "oneplus7",

          "oneplu6"

        ]

      },

      {

        "brand": "Samsung",

        "models": [

          "S7",

          "S6"

        ]

      }

    ]

  },

  {

    "OS": "IOS",

    "data": [

      {

        "brand": "Apple",

        "models": [

          "Iphone7",

          "Iphone6"

        ]

      }

    ]

  }

]


查看完整回答
反对 回复 2022-01-07
  • 1 回答
  • 0 关注
  • 132 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号