Javascript 如何为此设计模式?

Javascript 如何为此设计模式?,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我正在开发一个MERN应用程序。我需要设计相当简单的移动商店网站模式 数据应采用下面给出的格式 { [ "IOS":[ "Apple":[ { "model":"Iphone6" }, { "model":"Iphone7" } ] ], "Android":[ "Samsung":[ {

我正在开发一个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中为此设计模式?

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

我将按如下方式设置模式:

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)
};
[
  {
    "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"
        ]
      }
    ]
  }
]
根据架构插入文档后,我们可以使用以下聚合按操作系统和品牌进行分组:

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);
});
结果如下所示:

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)
};
[
  {
    "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"
        ]
      }
    ]
  }
]

这取决于你的需要。一个解决方案是收集
智能手机
,每个文档都是一部带有
OS-Brand-Model
字段的手机,谢谢@Weedoze的评论。在前端,我只想点击url一次,以获得在其上迭代的完整数据。也许我的想法是错误的。您能给出一个用于可视化的解决方案的json格式吗?您建议的数据格式不是有效的json;)不能在数组中使用键/值对。但除此之外,你还应该提供更多的信息,哪些信息是必需的,哪些信息是限制条件values@relief.melone基本要求是按照上面提到的json对品牌和型号进行分组。在每个操作系统平台下,我想对品牌及其各自的移动机型进行分组。因此,在前端,当我映射OS平台阵列时,它应该显示其中所有可用的品牌(例如:IOS)和相应的mob型号。然后再看下一个操作系统平台(例如:Android)及其品牌和型号