关系数据库设计到mongoDB/mongoose设计 [英] Relational database design to mongoDB/mongoose design

查看:62
本文介绍了关系数据库设计到mongoDB/mongoose设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始为新的node.js应用程序使用mongoDB和mongoose.在努力适应mongoDB/noSQL的思维方式(如非规范化和缺少外键关系)之前,仅使用过关系数据库.我有这个关系数据库设计:

I have recently started using mongoDB and mongoose for my new node.js application. Having only used relational databases before I am struggling to adapt to the mongoDB/noSQL way of thinking such as denormalization and lack of foreign key relationships. I have this relational database design:

**Users Table**

user_id
username
email
password

**Games Table**

game_id
game_name

**Lobbies Table**

lobby_id
game_id
lobby_name

**Scores Table**

user_id
game_id
score

因此,每个大厅都属于一个游戏,并且多个大厅可以属于同一游戏.用户对不同游戏的得分也不同.到目前为止,对于我的用户模式,我有以下内容:

So, each lobby belongs to a game, and multiple lobbies can belong to the same game. Users also have different scores for different games. So far for my user schema I have the following:

var UserSchema = new mongoose.Schema({
    username: {
        type: String,
        index: true,
        required: true,
        unique: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    }
});

所以我的问题是,如何将关系设计构造为mongoDB/mongoose模式?谢谢!

So my question is, how would I go about structing the relational design into mongoDB/mongoose schemas? Thanks!

编辑1

我现在已经尝试创建所有模式,但是我不知道这是否是正确的方法.

I have now tried to create all the schemas but I have no idea if this is the right approach or not.

var UserSchema = new mongoose.Schema({
    _id: Number,
    username: {
        type: String,
        index: true,
        required: true,
        unique: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    scores: [{ type: Schema.Types.ObjectId, ref: 'Score' }]
});

var GameSchema = new mongoose.Schema({
    _id: Number,
    name: String
});

var LobbySchema = new mongoose.Schema({
    _id: Number,
    _game: { type: Number, ref: 'Game' },
    name: String
});

var ScoreSchema = new mongoose.Schema({
    _user : { type: Number, ref: 'User' },
    _game : { type: Number, ref: 'Game' },
    score: Number
});

推荐答案

猫鼬的设计方式是,您可以相对轻松地对表进行建模,并根据您的 ref 填充关系数据在架构中定义.需要注意的是,您在填充时必须小心.如果人口过多或嵌套人口,则会遇到性能瓶颈.

Mongoose is designed in such a way that you can model your tables relationally with relative ease and populate relational data based on the ref you defined in the schema. The gotcha is that you need to be careful with populating. If you populate too much or nest your populations you will run into performance bottle necks.

您在 Edit 1 中使用的方法在很大程度上是正确的,但是您通常不希望基于 Number 或set来填充远程 ref 将模型的 _id 转换为 Number ,因为mongo使用它自己的哈希机制来管理 _id ,因此通常是 ObjectId带有 _id .示例如下所示:

Your approach in Edit 1 is largely correct however you usually don't want to populate a remote ref based on a Number or set the _id of a model to a Number since mongo uses it's own hashing mechanism for managing the _id, this would usually be an ObjectId with _id implied. Example as shown below:

var ScoreSchema = new mongoose.Schema({
    user : { type: Schema.Types.ObjectId, ref: 'User' },
    game : { type: Schema.Types.ObjectId, ref: 'Game' },
    score: Number
});

如果出于某种原因需要维护记录的数字ID,请考虑将其称为 uid 或与mongo/mongoose内部结构不冲突的名称.祝你好运!

If for some reason you need to maintain a number id for your records consider calling it uid or something that won't conflict with mongo / mongoose internals. Good luck!

这篇关于关系数据库设计到mongoDB/mongoose设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆