Inconsistency between object-oriented programming and relational database (Object-relational impedance mismatch)

Source: Internet
Author: User
Tags arrays data structures mongodb object model

Reprinted from: http://t.dbdao.com/archives/why-is-mongodb-wildly-popular.html

"If you show me the code and don't let me understand the data structure, I'll be confused." If I knew the data structure, I would obviously no longer have to look at your code more often. "– Written by Eric Raymond in" the Cathedral and the Bazaar:musings on Linux and Open Source by an accidental revolutionary ", 1997

Language innovation

The basic task of programming is to tell the computer how to do something. Because of this, many innovations in software development are language innovations that make it easier and more efficient for programmers to instruct the operation of a computer.

Although computers run in binary, we do not communicate with them in this way. Every 10 years, the higher-level programming language continues to emerge, and the programmer's self-expression capabilities continue to grow. These innovations include improvements to how data structures are expressed and how the algorithms are expressed.

Inconsistency between object-oriented programming and relational database (Object-relational impedance mismatch)

Almost all modern programming languages support object-oriented (OO), and when we build an object model with code, we typically use the combination of primitive types (integers, strings, etc.), arrays, and objects.

Although the way in which each language handles details is different, the idea of nesting object structures has become a common language that we use to describe "things".

The data structures we use to persist data do not evolve at the same rate. In the past 30 years, the primary data structure for persistent data has been the table – a rowset consisting of columns containing scalar values (integers, strings, and so on). This is a relational database of the world, starting in 1980, its transactional, fast query, space use efficiency, more than the same era of other database systems, and it also has a strong Oracle sales team.

The difference between object-oriented code modeling and persisted data (through tables) has been a source of confusion for many programmers. Over the past thousands of years, people have been changing the shape of the data, that is, the physical form and the relationship between the form of a large number of efforts to invest.

The tools of the so-called object-relational mapping system (object-relational Mapping Systems, ORMs) exist in every object-oriented language. However, with these tools, almost all programmers complain about the O/R mapping, which is a time-consuming chore in any way.

Ted Neward's words hit its key: "Object-relational mapping work is as difficult as the Vietnam War." ”

viet-300x230

In the 90 's, the object database had been tried, but there was no real alternative to the relational database technology appeared. The file-based database, especially MongoDB, is the first successful web-era object store. Because of this, it will be the first major language innovation in representing persistent data structures for a long time. In contrast to a planar, two-dimensional table, the corresponding record is stored in a collection of rich objects, recursive objects, and n-dimensional objects (aka files).

An example: Blog

For blog posts. You might have a class/object structure when you do a code-modeling blog post, but if you use a relational database to store your blog data, each entry is uploaded to several tables (dbdao.com).

blog-relational-1024x578

As a developer, you need to know how to convert between the "post" object and the group table where they are placed in the relational model.

A different approach.

With MongoDB, blog posts can be stored in a single collection, and each entry looks like this:

{
_id:1234,
Author: {name: "Bob Davis", Email: "Bob@bob.com"},
Post: "In these troubled times I like to ...",
Date: {date: "2010-07-12 13:23UTC"},
Location: [-121.2322, 42.1223222],
rating:2.2,
Comments: [
{User: "jgs32@hotmail.com",
Upvotes:22,
Downvotes:14,
Text: "Great point! I Agree "},
{User: "holly.davidson@gmail.com",
upvotes:421,
Downvotes:22,
Text: "You are a moron"}
],
Tags: ["Politics", "Virginia"]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
_id:1234,
Author: {name: "Bob Davis", Email: "Bob@bob.com"},
Post: "In these troubled times I like to ...",
Date: {date: "2010-07-12 13:23UTC"}, Location: [ -121.2322, 42.1223222], rating:2.2, comments: [{User: "Jgs32@hotmail . com ", upvotes:22, downvotes:14, Text:" Great point! I Agree "}, {User:" holly.davidson@gmail.com ", upvotes:421, downvotes:22, Text:" You are a moron "}], Tags: [" politic S "," Virginia "]} 1 2 3 4 5 6 7 8 9 (_id:1234, Author: {name:" Bob Davis ", Email:" bob@ Bob.com "}, Post:" In these troubled times I like to ... ", Date: {date:" 2010-07-12 13:23UTC "},
Location: [-121.2322, 42.1223222],
rating:2.2,
Comments: [
{User: "jgs32@hotmail.com",
Upvotes:22,
Downvotes:14,
Text: "Great point! I Agree "},
{User: "holly.davidson@gmail.com",
upvotes:421,
Downvotes:22,
Text: "You are a moron"}
],
Tags: ["Politics", "Virginia"]
}
With a document database, your data storage is almost the same as it does in your program. There is no complex mapping work (although people often choose to bind objects to instances of a particular code class).

What are the advantages of MongoDB?

MongoDB is doing well when modeling object instances for the most modern web applications, whether the app is for consumers or the enterprise:

Accounts and user profiles: Easily store address arrays

CMS:MONGODB's flexible model has advantages over heterogeneous collections of content types

form data: MongoDB can make form data structure easier

Blog/user-generated content: Enables data from complex relationships to be set on one object

Message: Easily change the message metadata for each message or message type without having to maintain a scattered set of tables

System configuration: Good Configuration Value object graph, which is very common in MongoDB

Record any type of data: Structured log data is the future trend

Graphics: Objects and pointers – a perfect combination

Location-based data: MongoDB understands geospatial coordinates and natively supports geo-spatial indexing

Outlook: Data will become a portal (dbdao.com)

Eric Raymond's famous quote in the book "The Cathedral and the Bazaar" (The mythical Man-Month, which was rewritten from earlier Fred Brooks):

"If you show me the code and don't let me understand the data structure, I'll be confused." If I knew the data structure, I would obviously no longer have to look at your code more often. ”

The data structure embodies the essence of our program and philosophy. Therefore, as programmers, we support continuous innovation, which allows us to define expression data structures more easily for application domain modeling.

People often ask me why MongoDB is so popular. I told them it was a data structure problem. (dbdao.com)

While MongoDB may be hampered by the scalability of other NoSQL database technologies, the rapid success of MongoDB is largely due to its innovation as a data structure store that is simple and expressive in modeling the "things" at the core of our application. For this reason, MongoDB, or some very similar technology, will become the dominant database paradigm for operational data storage, complemented by a relational database as a specialized tool.

Maintaining a consistent base data model in both code and database is a better approach in most usage situations, because it greatly simplifies the application development tasks and eliminates complex mapping relationships. In a JSON-based document database It seems obvious (if not, in the future) to do the right thing, just as 10gen people are already doing, which represents a major innovation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.