如何为多个城市创建模式设置?

How to create schema setup for multiple cities?

我打算用 mern 堆栈创建一个 to/do 应用程序,我想在应用程序中包含多个国家和城市。

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

然后我可以将这个模式用于 x 个国家,因此当我需要为加拿大、温哥华的用户加载 posts 时,posts 将存储在一个非常特定的位置,使搜索非常容易。

如有此类设置经验,请指教,谢谢

尝试创建猫鼬模式如下。

这对您的要求会有帮助

//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);