Document (JSON) usage model

Time:2020-12-3

1、 Background

E. F. Codd first put forward the relational model of database system in 1970. From then on, he initiated the research of relational database method and relational data theory, which laid a theoretical foundation for database technology, and database technology began to flourish. With the commercial database management systems released by several major database manufacturers, almost all of them support relational data model, and the database technology is gradually unified to be dominated by relational database.

After 2001, with the rapid development of Internet technology and the rapid expansion of data, human beings have gradually entered the era of big data. Big data has brought severe challenges to the traditional data management methods. Relational databases are difficult to meet the needs of big data management in terms of capacity, performance and cost. NoSQL database has gained great advantages in scalability, model flexibility, economy and accessibility by compromising the strict data consistency management of relational database. It can better adapt to the needs of big data application and become the most important data management technology in the era of big data.

2、 Product introduction

Sequoiadb is a new generation of distributed document database. Its data model is document model (JSON), not traditional relational data model. Relational model uses two-dimensional table to represent the relationship between entities. In data modeling, the data objects need to be split, and then their information is stored in the corresponding table, and the tables are connected when necessary. While the tsutsuga database is stored in a single document, supporting array and document nesting. The information that needs to be split in the relational model can be directly represented by a document.

JSON (JavaScript object notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. It is a plain text format and supports nested structures and arrays.

JSON has the following forms:

1. Object is an unordered collection of key value pairs, beginning with “{” (left brace) and ending with “}” (right brace). Each element name is followed by a ‘:’ (colon); the elements are separated by “,” (comma);
Document (JSON) usage model

2. An array is an ordered collection of values, beginning with “[” (left square bracket) and ending with “]” (right bracket). Values are separated by “,” (comma);
Document (JSON) usage model

3. The value can be a string wrapped in double quotation marks, numeric value, object, array, true, false, null, and the data structure (such as date, time, etc.) unique to sequoiadb database.
Document (JSON) usage model

3、 Scene design

The application scenarios of document model are very extensive. Here, we introduce the use of document model by taking shopping cart, which is a familiar and often used scenario.
Shopping cart is a virtual basket for storing goods of interest during online shopping. When you open the shopping cart, you usually want to see the general information of each product, such as the name of the store, the name of the product, price, quantity, etc. if the product is clothing, it may also need to display the size, color and other attribute information.
When we use document model to express shopping cart application, a reference data model is as follows:

{
  "_id": {
    "$oid": "57b44b2b2b57085321000001"
  },
  "items": [    
    {
      "shopid": 8224,
      "picture": "http://www.sequoiadb.com/product.jpg",
      "amount": 1,
      "price": "117.59",
      "itemname": "Coffee",
      "itemid": 194987
    },
    {
      "shopid": 9291,
      "attribute": [
        {
          "color": "Blue",
          "size": "M"
        },
        {
          "color": "Pink",
          "size": "M"
        }
      ],
      "picture": "http://www.sequoiadb.com/product.jpg",
      "amount": 2,
      "price": "17.63",
      "itemname": "T-shirt",
      "itemid": 543514
    }
  ],
  "isactive": true,
  "uid": 123456
}

Each user corresponds to a document. The document consists of several fields such as user ID, status and shopping cart items. The shopping cart is a nested document, which contains the goods in the corresponding user’s shopping cart. Each commodity has information such as name, price and quantity. You can also add different fields for different types of goods according to your needs. For example, the “attribute” field here is used to store the attribute information of clothing goods, such as color and size. Of course, the same kind of goods can have different attribute information with different numbers and contents, which can be handled flexibly.

4、 Implementation of JSON API

For a shopping cart, the common operations are: add goods, delete goods, increase the number of goods, modify commodity properties, commodity statistics. In the document model, we provide a complete set of API to represent a series of operations related to shopping cart.

4.1 adding goods

When we want to add a book with a price of 99 yuan in the shopping cart, we can use the following UPDATE statement:

db.mall.cart.update({
  $push:{
    items:{
      shopid:6666,
      amount:1,
      price:"99.00",
      itemname:"Book",
      itemid:206053
    }
  }
},
{
  uid:123456
})

Among them{ uid:123456 }For the updated matching criteria, it represents the user with the user ID of 123456, and $push means an element is added to the end of the array. The syntax of $push is:

{
  $push:{
    < fieldname1 >: < value1 >,
    < fieldname2 >: < Value2 >,
    ...
  }
}

$push inserts the given value (< value1 >) into the target array (< fieldname1 >), and the operands must be fields of array type.

4.2 delete item

You can also use the update statement if you want to remove specific items from the shopping cart. Different from adding the update character $push, deleting items in the shopping cart uses the $pull update character, and the corresponding statement is as follows:

db.mall.cart.update({
  $pull:{
    items:{
      shopid:6666,
      amount:1,
      price:"99.00",
      itemname:"Book",
      itemid:206053
    }
  }
},
{
  uid:123456
})

The same thing{ uid:123456 }Represents a user with user ID 123456. $pull means that matching array elements are cleared from the array.
The syntax of $pull is as follows:

{
  $pull: {
    < fieldname1 >: < value1 >,
    < fieldname2 >: < Value2 >,
    ...
  }
}

$pull clears the specified value (< value1 >, < Value2 >,…) of the specified array object (< fieldname1 >, < fieldname2 >,…). The operands must be fields of array type. If the specified array object does not exist in the record, skip and do nothing; if the specified value does not exist in the array object, do nothing.
In addition, the Taxus database supports the $replace syntax without changing_ ID (the unique identifier automatically generated by the database), the corresponding statement is as follows:

db.mall.cart.update( {
  $replace:{
    uid:123456,
    items:[{
      itemid:5856,
      itemname:"Cup",
      amount:1, 
      price:"69.00",
      shopid:8224
    }
          ]
  }
}, 
{  
  uid:123456 
} )

The effects before and after modification are as follows:

Document (JSON) usage model

Note that two JSON objects are used as parameters when using find. This is the special syntax of the find function of the Tsutsuki database. The first JSON object represents the query condition, representing the user ID uid of 123456. The second JSON object represents the selector, which controls the fields returned. It can be understood as the field name after the select statement in the standard SQL syntax, indicating the return You only need to include the specified fields after select.
The syntax of $replace is as follows:

{
  $replace:{
    < fieldname1 >: < value1 >,
    < fieldname2 >: < Value2 >,
    ...
  }
}

The $replace operation is to replace all documents with “{< fieldname1 >: < value1 >, < fieldname2 >: < Value2 >,…}”. Except to keep the original_ In addition to ID, the contents of the original document will be empty and replaced with “{< fieldname1 >: < value1 >, < fieldname2 >: < Value2 >,…}”.

4.3 increase the quantity of goods

When we need to modify the quantity of a certain item in the shopping cart, we can use the update statement with a specific update character to complete. The implementation statement is as follows:

db.mall.cart.update({
  $inc:{
    "items.0.amount":1
  }
},
{
  "uid":123456
})

Here we use the update character $Inc to increase the amount of the first item in the shopping cart by 1. Note the syntax of the first item in the shopping cart here: items.0.amount, where 0 is the ordinal number of the items in the shopping cart, and the array elements are counted from 0.
The syntax of $Inc is as follows:

{
  $inc:{
    < fieldname1 >: < value1 >,
    < fieldname2 >: < Value2 >,
    ...
  }
}

The $Inc operation is to add the specified “< value >” to the specified “< field name >”.
In addition to $Inc, you can also use $set to modify the quantity of goods in the shopping cart

{
  $set:{
    < fieldname1 >: < value1 >,
    < fieldname2 >: < Value2 >,
    ...
  }
}

The $set operation updates the specified “< field name >” to the specified “< value >”.

4.4 Commodity Statistics

How to get the total number of items in the shopping cart of a user? Or how to get the quantity and total price of the selected items in the database when the buyer selects multiple items when preparing to check out? To get this information, you need to implement it with JavaScript code.
Document (JSON) usage model

In this way, we can get the unit price, quantity and other information of each commodity in the shopping cart. According to different application requirements, we can query the information of goods in the database conveniently and process it.

5、 Conclusion

Sequoiadb adopts the data model (object storage) defined by JSON, which saves the objects in the program in the database in a native way, and can retrieve and match the attributes or sub objects in it. It can greatly weaken the complex relational model, accelerate the development of application, and reduce the operation and maintenance cost of the system.

Download the latest version of sequoiadb database 2.6

Sequoiadb database technology blog

Sequoiadb Tsuga database community