在mongodb上表示多语言数据库的最佳方法 [英] Best way to represent multilingual database on mongodb

查看:165
本文介绍了在mongodb上表示多语言数据库的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL数据库来支持多语言网站,其中的数据表示如下:

I have a MySQL database to support a multilingual website where the data is represented as the following:

table1

id
is_active
created

table1_lang

table1_lang

table1_id
name
surname
address

在mongo数据库上实现相同目的的最佳方法是什么?

What's the best way to achieve the same on mongo database?

推荐答案

您可以设计可以引用或嵌入文档的架构.让我们看一下嵌入式文档的第一个选项.通过上述应用程序,您可以将信息存储在文档中,如下所示:

You can either design a schema where you can reference or embed documents. Let's look at the first option of embedded documents. With you above application, you might store the information in a document as follows:

// db.table1 schema
{
    "_id": 3, // table1_id
    "is_active": true,
    "created": ISODate("2015-04-07T16:00:30.798Z"),
    "lang": [
        {
            "name": "foo",
            "surname": "bar",
            "address": "xxx"
        },
        {
            "name": "abc",
            "surname": "def",
            "address": "xyz"
        }
    ]
}

在上面的示例架构中,您实际上已经将table1_lang信息嵌入到主table1文档中.这种设计有其优点,其中之一就是数据局部性.由于MongoDB将数据连续存储在磁盘上,因此将所需的所有数据放在一个文档中可确保旋转的磁盘将花费更少的时间查找磁盘上的特定位置.如果您的应用程序经常访问table1信息和table1_lang数据,那么您几乎肯定会想走嵌入式路线.嵌入式文档的另一个优点是写入数据时的原子性和隔离性.为了说明这一点,假设您要删除一个文档,该文档的lang键为"name",其值为"foo",这可以通过一个(原子的)操作来完成:

In the example schema above, you would have essentially embedded the table1_lang information within the main table1document. This design has its merits, one of them being data locality. Since MongoDB stores data contiguously on disk, putting all the data you need in one document ensures that the spinning disks will take less time to seek to a particular location on the disk. If your application frequently accesses table1 information along with the table1_lang data then you'll almost certainly want to go the embedded route. The other advantage with embedded documents is the atomicity and isolation in writing data. To illustrate this, say you want to remove a document which has a lang key "name" with value "foo", this can be done with one single (atomic) operation:

db.table.remove({"lang.name": "foo"});

有关MongoDB中数据建模的更多详细信息,请阅读文档 数据建模简介 ,特别是

For more details on data modelling in MongoDB, please read the docs Data Modeling Introduction, specifically Model One-to-Many Relationships with Embedded Documents

另一个设计选项是引用遵循规范化架构的文档.例如:

The other design option is referencing documents where you follow a normalized schema. For example:

// db.table1 schema
{
    "_id": 3
    "is_active": true
    "created": ISODate("2015-04-07T16:00:30.798Z")
}

// db.table1_lang schema
/*
1
*/
{
    "_id": 1,    
    "table1_id": 3,
    "name": "foo",
    "surname": "bar",
    "address": "xxx"
}
/*
2
*/
{
    "_id": 2,    
    "table1_id": 3,
    "name": "abc",
    "surname": "def",
    "address": "xyz"
}

以上方法为执行查询提供了更大的灵活性.例如,要检索ID为3的主父实体table1的所有子table1_lang文档将很简单,只需针对集合table1_lang创建查询:

The above approach gives increased flexibility in performing queries. For instance, to retrieve all child table1_lang documents for the main parent entity table1 with id 3 will be straightforward, simply create a query against the collection table1_lang:

db.table1_lang.find({"table1_id": 3});

当您使用非常不可预测的关系建立一对多关系时,上述使用文档引用方法的规范化架构也具有优势.如果每个给定table实体每个实体都有成百上千的table_lang文档,那么就空间限制而言,嵌入会遇到很多挫折,因为文档越大,使用的RAM越多,而MongoDB文档的大小限制很大16MB.

The above normalized schema using document reference approach also has an advantage when you have one-to-many relationships with very unpredictable arity. If you have hundreds or thousands of table_lang documents per give table entity, embedding has so many setbacks in as far as spacial constraints are concerned because the larger the document, the more RAM it uses and MongoDB documents have a hard size limit of 16MB.

一般的经验法则是,如果您的应用程序的查询模式是众所周知的,并且数据倾向于仅以一种方式访问​​,则嵌入式方法会很好地工作.如果您的应用程序以多种方式查询数据,或者您无法预期数据查询模式,那么更规范的文档引用模型将适合这种情况.

The general rule of thumb is that if your application's query pattern is well-known and data tends to be accessed only in one way, an embedded approach works well. If your application queries data in many ways or you unable to anticipate the data query patterns, a more normalized document referencing model will be appropriate for such case.

参考:

MongoDB应用的设计模式:领先的NoSQL数据库的实际使用案例Rick Copeland

MongoDB Applied Design Patterns: Practical Use Cases with the Leading NoSQL Database By Rick Copeland

这篇关于在mongodb上表示多语言数据库的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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