Mongodb design method and skill examples



Mongodb is a popular database that can work without any form schema schema. Data is stored in a JSON like format and can contain different types of data structures. For example, in the same collection collection, we can have two document documents:

  id: '4',
  name: 'Mark',
  age: '21',
  addresses : [
    { street: '123 Church St', city: 'Miami', cc: 'USA' },
    { street: '123 Mary Av', city: 'Los Angeles', cc: 'USA' }

  id: '15',
  name: 'Robin',
  department: 'New Business',
  example: '[email protected]'

In order to take full advantage of mongodb, you must understand and follow some basic database design principles. Before we explain the design method, we must first understand the structure of mongodb’s stored data.

1、 How data is stored in mongodb

Different from the traditional RDBMS relational database, mongodb does not have the concepts of table, row and column. It stores data in collections, documents, and fields. The following figure illustrates the relationship between the structures of the RDBMS analogy:

2、 Database design skills and tips

2.1. Standardized storage and non standardized storage

Because mongodb uses documents to store data, it is important to understand the concepts of “normalized storage” and “non normalized storage.”.

Normalized storage: – normalization means storing data in multiple collections and designing associations between them. After the data is saved, it is easier to update the data. However, when reading data, the shortcomings of standardized storage appear. If you want to find data from multiple collections collections, you have to perform multiple queries, which slows reading data. (for example: store the title, author and content of the web page in different collections)

Denormalized storage: – this way stores several object data in a nested manner into a single document. It performs better when reading data, but slower when writing. This way of storing data will also take up more space. (for example: store the title, author and content of the web page in the same collection respectively)

So before choosing between two ways to store data, evaluate how your application database is used.

If you have data that doesn’t need to be updated frequently, immediate consistency of updates is not important, but you need good performance when reading, then denormalization may be a wise choice. (for example, once the author of our blog posts is saved, he will hardly make frequent modifications, but he is faced with frequent reading operations by readers.)

If the document data in the database needs to be constantly updated, and you want good performance when writing, you may need to consider normalized storage. (e.g. business systems requiring frequent data modification)

2.2. One to many relationship

Compared with RDBMS, modeling one to many relationships in mongodb requires more fine-grained design. Many beginners fall into the trap of embedding the document array into the parent document. As we mentioned above, it’s important to know when to normalize or denormalize storage. Therefore, designers need to consider whether the cardinality of the relationship is “one to a few” or “one to many”? Each relationship will have a different modeling approach.

For example: the following “one to a few” modeling example. The best modeling method is to embed several addresses in the parent document (persopn)

> db.person.findOne()
 name: 'Mark Kornfield',
 ssn: '1223-234-75554',
 addresses : [
   { street: '123 Church St', city: 'Miami', cc: 'USA' },
   { street: '123 Mary Av', city: 'Los Angeles', cc: 'USA' }

In the “one to many” example, we will consider designing two sets, the product products collection and the parts parts collection. Each part has an objectid that will appear in the reference to the product collection. This design can make reading and writing performance more efficient.

  _id : ObjectID('AAAA'),
  partno : '1224-dsdf-2215',
  name : 'bearing',
  price: 2.63

> db.products.findOne()
  name : 'car',
  manufacturer : 'Ford',
  catalog_number: 2234,
  parts : [   // array of references to Part documents
    ObjectID('AAAA'),  // reference to the bearing above
    ObjectID('F17C'),  // reference to a different Part
    // etc

2.3. Design pattern visualization

Although mongodb is schemaless “schemaless”, there are still ways to visualize collection collections as graphs. Being able to view design drawings will have a significant impact on the way you understand and design mongodb.

Dbschema is a good tool for visual design. As shown in the figure below, it infers the schema by reading collections and documents. In addition, you can modify the objects in the diagram with a single click. In dbschema, you can also create a foreign key for mongodb, of course only locally for design purposes.

2.4. Intelligent index

In order to maintain the good performance of the database, it is necessary to establish intelligent index, which will simplify the write and read operations. It is important to know the index advantages and limitations of mongodb. The memory limit reserved by mongodb for sorting operation is 32MB. If you do not use the index, the database will be forced to hold all sorted documents in memory during sorting. If the limit of 32m is reached, the database will return an error or empty set.


A thorough understanding of mongodb and a clear understanding of what the database wants to achieve are the secrets of good database design.


This article about mongodb design methods and techniques of this article introduced here, more related mongodb design methods and skills content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you will support developeppaer more in the future!

Recommended Today

Solutions to leetcode problems and knowledge points 1. Sum of two numbers

Title Link 1. Two Sum  difficulty: $/ color {00965e} {easy}$ Knowledge points 1. Hash (hash) function The classic hash function times33 is widely used, and the core algorithm is as follows: hash(i) = hash(i-1) * 33 + str[i] Laruence has an article about:Hash algorithm in PHP 2. Hash collision processing method 2.1 chain address method […]