【问题标题】:How to create schema setup for multiple cities?如何为多个城市创建架构设置?
【发布时间】:2020-06-14 06:59:42
【问题描述】:

我要使用 mern 堆栈创建一个 to/do 应用程序,我希望应用程序中有多个国家和城市。

对于项目的设计,我是否应该创建一个包含国家名称和城市数组的主模式,并为帖子类型嵌入以下数组。

然后我可以将此模式用于 x 个国家/地区,因此当我需要为加拿大温哥华的用户加载帖子时,帖子将存储在一个非常特定的位置,从而使搜索变得非常容易。

如果你有这种设置的经验,请指教,谢谢。

【问题讨论】:

    标签: mongodb express mongoose database-design mongoose-schema


    【解决方案1】:

    尝试如下创建猫鼬模式。

    这将有助于您的要求

    //Country Schema
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    var Country = new Schema({
        CountryID: { type: String, default: "" },
        Country_Name: { type: String, default: "" },
        Country_Code: { type: String, default: "" },//Mobile
        Country_Currency: { type: String, default: "" },//Business or Ecommerce
        location: { //for Map Purpose
            longitude: Number,
            latitude: Number
        },
        Point: { type: [Number], index: '2d' },
    }, { collection: 'Country' });
    Country.index({ Point: '2dsphere' });
    module.exports = mongoose.model('Country', Country);
    
    
    //City Schema
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    var City = new Schema({
        CityID: { type: String, default: "" },
        CountryID: { type: String, default: "" },
        City_Name: { type: String, default: "" },
        location: { //for Map Purpose
            longitude: Number,
            latitude: Number
        },
        Point: { type: [Number], index: '2d' },
    }, { collection: 'City' });
    City.index({ Point: '2dsphere' });
    module.exports = mongoose.model('City', City);
    

    【讨论】:

    • 先生,我们可以在国内建立城市吗?例如,如果我要创建 5 个国家/地区的应用程序,我可以创建 5 个模式,然后对于城市数组,我可以列出用户可以选择的所有城市,因此当他们创建帖子时,如果他们进入我的应用程序选择 I我来自加拿大,温哥华,然后对于帖子页面,我将有加拿大模式,并将自动填充他们的城市,这样他们就可以填写表格的其余部分,当我为加拿大的路线做 GET 时,我们可以 GET Canada 收藏,并取用户想要的所有城市,我正在尝试针对许多城市进行优化。
    • 对于我的需要,它只是城市和国家,然后我可以在这些位置内搜索任何特定需求,但我想让它容易知道在哪里搜索,所以如果我有 100 万不同城市的帖子,我可以让它更有效率
    • 在具有唯一国家 ID 的国家模型中插入加拿大数据。
    • 使用加拿大国家/地区ID,在城市示例多伦多内添加具有唯一城市ID的城市
    • 为我们的架构只拥有城市不是更好吗,我在考虑也许没有必要拥有 country ,然后是 nest city ,因为对于 mongodb 来说,在 40 个城市中搜索 1 个城市很容易例如,城市,而不是在国家模型中搜索 10 个城市中的 1 个,这有意义吗?
    猜你喜欢
    • 1970-01-01
    • 2015-07-23
    • 2013-10-23
    • 1970-01-01
    • 2016-10-25
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多