亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何為此設計架構?

我如何為此設計架構?

慕絲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 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號