Mysql 如何在mongodb中存储关系数据

Mysql 如何在mongodb中存储关系数据,mysql,mongodb,nosql,Mysql,Mongodb,Nosql,我想存储与地址相关的数据。我有很多国家,每个国家都有州,每个国家都有城市。在像mysql这样的关系数据库中,我将以以下方式存储 CREATE TABLE countries (name VARCHAR(20), id int(20)...few more attributes); CREATE TABLE states (name VARCHAR(20), id int(20), countryId int(20) ...few more attributes); CREATE TABLE ci

我想存储与地址相关的数据。我有很多国家,每个国家都有州,每个国家都有城市。在像mysql这样的关系数据库中,我将以以下方式存储

CREATE TABLE countries (name VARCHAR(20), id int(20)...few more attributes);
CREATE TABLE states (name VARCHAR(20), id int(20), countryId int(20) ...few more attributes);
CREATE TABLE cities (name VARCHAR(20), id int(20), stateId int(20)...few more attributes);
我总是在要求城市时了解国家,在要求国家时了解国家。在mongodb中存储此类数据的理想方法是什么。在mongodb中我还可以想到一种方法

var Country = new Schema({
    name: {
        type: String,
        default: '',
        trim: true
    },
    states: [{
        type: Schema.ObjectId,
        ref: 'Testrun'
    }]
});
var State = new Schema({
    name: {
        type: String,
        default: '',
        trim: true
    },
    cities: [{
        type: Schema.ObjectId,
        ref: 'Testrun'
    }]
});
var City = new Schema({
    name: {
        type: String,
        default: '',
        trim: true
    }

});

我觉得如果我像mysql那样存储,更新、创建或删除就会容易得多。

“如何在MongoDB中存储关系数据?”听起来就像“如何在圆孔中存储方钉?”。一个人将不得不忍受一些虐待,才能被敲入状态,要么是钉子,要么是洞。您希望它是哪一个?MongoDB不是关系数据库。如果您想像在MySQL中那样存储您的国家、州和城市,那么就使用MySQL。在mongo中,有两种方式:1)将三个表存储为三个单独的集合,或2)将城市嵌套在国家/地区下的州下(如示例所示)。至于哪种方式更好,完全取决于您的用例。