欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongodb数据建模设计_MongoDB中的数据建模

程序员文章站 2024-01-27 21:16:16
...

mongodb数据建模设计

MongoDB中的数据建模 (Data Modelling in MongoDB)

As explained in the earlier lessons, data in MongoDB is schema-less, which means there is no need of defining a structure for the data before insertion. Since, MongoDB is a document based database, any document within the same collection is not mandatory to have same set of fields or structure. This helps in easily mapping the documents with the entity objects. In general, the documents in a collection of MongoDB will always share the same data structure(recommended for best performance, not mandatory).

如先前课程中所述,MongoDB中的数据是无架构的,这意味着在插入之前无需为数据定义结构。 由于MongoDB是基于文档的数据库,因此同一集合中的任何文档都不必具有相同的字段或结构集。 这有助于轻松地将文档与实体对象映射。 通常,MongoDB集合中的文档将始终共享相同的数据结构(为获得最佳性能而推荐,而非强制性)。

The vital factor or challenge in modelling the data in a given database is load balancing and hence ensuring the performance aspect effectively. It is a mandate while modelling the data to consider the complete usage of data (CRUD operations) along with how the data will be inherited as well.

在给定数据库中对数据建模的关键因素或挑战是负载平衡,从而有效地确保性能。 在对数据进行建模时,必须考虑数据的完整使用情况(CRUD操作)以及如何继承数据。

There are 2 ways in which the relationships between the data can be established in MongoDB:

在MongoDB中可以通过两种方式建立数据之间的关系:

  • Reference Documents

    参考文件
  • Embedded Documents

    嵌入式文件

MongoDB:参考文档 (MongoDB: Referenced Documents)

This is one of the ways to implement the relationship between data stored in different collections. In this, a reference to the data in one collection will be used in connecting the data between the collections.

这是实现存储在不同集合中的数据之间的关系的一种方法。 在这种情况下,对一个集合中数据的引用将用于在集合之间连接数据。

Consider 2 collections books and authors as shown below:

考虑如下所示的两booksauthors

{
  title: "Java in action",
  author: "author1",
  language: "English",
  publisher: {
             name: "My publications",
             founded:1990,
             location: "SF"
            }
}

{
  title: "Hibernate in action",
  author: "author2",
  language: "English",
  publisher: {
             name: "My publications",
             founded:1990,
             location: "SF"
            }
}

In the above example, the publisher data is repeated. In order to avoid this repetition, we can add references of the book to the publisher data instead of using entire data of publisher in every book entry, as shown below:

在上面的示例中, publisher数据被重复。 为了避免这种重复,我们可以将图书的引用添加到出版商数据中,而不是在每个图书条目中都使用出版商​​的全部数据,如下所示:

{
  name: "My Publciations",
  founded:1980,
  location: "CA",
  books: [111222333,444555666, ..]
}

{
   _id:111222333,
   title: "Java in action",
   author: "author1",
   language: "English"
}

{
  _id:444555666,
  title: "Hibernate in action",
  author: "author2",
  language: "English"
}

This can be done the other way round as well, where in one can reference the publisher id in the books data, your choice.

也可以用另一种方法来完成此操作,其中您可以选择在书本数据中引用出版商ID

MongoDB:嵌入式文档 (MongoDB: Embedded Documents)

In this, one collection will be embedded into another collection. Consider 2 collections student and address. Let us see how the address can be embedded into student collection. Below is an example embedding single address to student data.

在这种情况下,一个集合将被嵌入到另一个集合中。 考虑2个集合的studentaddress 。 让我们看看如何将地址嵌入到学生收藏中。 下面是将单个地址嵌入学生数据的示例。

{
  _id:123,
  name: "Student1"
}

{
  _studentId:123,
  street: "123 Street",
  city: "Bangalore",
  state: "KA"
}

{
  _studentId:123,
  street: "456 Street",
  city: "Punjab",
  state: "HR"
}

Embedding multiple addresses can also be done. See the below example :

也可以嵌入多个地址。 请参阅以下示例:

{
  _id:123,
  name: "Student1"
  addresses: [
      	{
             	street: "123 Street",
   		city: "Bangalore",
   		state: "KA"
        },

        {
   		street: "456 Street",
   		city: "Punjab",
   		state: "HR"
        }
    ]
}

MongoDB中的数据建模示例 (Example of Data Modelling in MongoDB)

Let us consider a simple example of building a student database in a college. Assume there are 3 models – Student, Address and Course. In a typical RDBMS database, these 3 models will be translated into 3 tables as shown below:

让我们考虑一个在大学中建立学生数据库的简单示例。 假设有3种模型- StudentAddressCourse 。 在典型的RDBMS数据库中,这3个模型将转换为3个表,如下所示:

mongodb数据建模设计_MongoDB中的数据建模

Hence, from the above model, if a student details has to be added, then entries should be made in all the 3 tables!!

因此,根据上述模型,如果必须添加学生详细信息,则应在所有3个表中进行输入!

Let us see, how the same data can be modelled in MongoDB. In MongoDB, schema design will have only collection one Student and will have the below structure.

让我们看看如何在MongoDB中建模相同的数据。 在MongoDB中,架构设计将仅收集一个Student ,并具有以下结构。

{
   _id: 123,
   firstName: 'Test',
   lastName: 'Student',
   address :[{
           City: 'Bangalore',
           State: 'Karnataka',
           Country: 'India'
        }
    ],
   Course: 'MCA'
}

In MongoDB, data related to all the 3 models will be shown under one Collection !!

在MongoDB中,与这3个模型有关的数据将显示在一个Collection下!

MongoDB provides with multiple ways of modelling your data. Now you know how to do that.

MongoDB提供了多种建模数据的方式。 现在您知道该怎么做了。

NOTE : Fieldnames in a collection like firstName and lastName etc in above examples also use memory, may 10-20 bytes or so. But when the dataset is very large, this can add up to a lot of memory. Hence it is adviced, for large datasets, use short fieldnames to store data in collections, like fname instead of firstName.

注意:上面示例中的firstNamelastName等集合中的字段名也使用内存,可能为10-20字节左右。 但是,当数据集非常大时,这可能会增加大量内存。 因此,建议对于大型数据集,使用短字段名将数据存储在集合中,例如fname而不是firstName

翻译自: https://www.studytonight.com/mongodb/data-modelling-in-mongodb

mongodb数据建模设计