存儲JSON的最佳方法,其中SQL似乎是不必要的

[英]Best way to store JSON where SQL seems unnecessary


I've created an angularjs webapp which allows you to interact with a large JSON object to change an on screen configuration. For example, click a button and...

我創建了一個angularjs webapp,它允許您與大型JSON對象進行交互以更改屏幕配置。例如,單擊按鈕並...

{'open': true, 'color': 'red', 'beef': 'jerky'} 

changes to

改變為

{'open': true, 'color': 'blue', 'beef': 'cake'} 

... and the changes you've made are reflected in a visible configuration on screen.

...並且您所做的更改將反映在屏幕上的可見配置中。

I've made a backend login which allows people to create an account linked to their email with all their data in an mySQL database.

我做了一個后端登錄,允許人們在mySQL數據庫中創建一個與他們的電子郵件鏈接的帳戶及其所有數據。

The goal is that a user would create a configuration of the JSON object, 'save' it and then be able to access it later. With angularjs I presumably could parse all this JSON into a gigantic table, but...

目標是用戶將創建JSON對象的配置,“保存”它然后能夠以后訪問它。使用angularjs我可能會將所有這些JSON解析成一個巨大的表,但......

  • I do not need to query the JSON with SQL ever (the data is only used by the front-end code as one big block)
  • 我不需要用SQL查詢JSON(數據僅被前端代碼用作一個大塊)
  • I may change how the JSON is structured/how many options are within it over time, I'm imagining that would make a gigantic custom table an extra thing to have to change
  • 我可能會改變JSON的結構方式/隨着時間的推移有多少選項,我想這將使一個巨大的自定義表成為必須改變的額外的事情

Would it be better for me to simply store that big block of JSON as a string in one column? Is this bad practice? Will I theoretically have a limit on the stringlength allowed in one table cell?

對於我來說,將一大塊JSON作為字符串存儲在一列中會更好嗎?這是不好的做法嗎?理論上我是否會限制一個表格單元格中允許的stringlength?

2 个解决方案

#1


1  

For dynamic configuration JSON objects, it is highly recommended that you parse it and store it as a string.

對於動態配置JSON對象,強烈建議您解析它並將其存儲為字符串。

There is of course a maximum length for the string you can store in one cell, however it is extremely large (any huge JSON object would fit).

當然,您可以在一個單元格中存儲的字符串的最大長度,但它非常大(任何巨大的JSON對象都適合)。

Hope this helps

希望這可以幫助

#2


1  

Trying to help...

試圖幫助......

There are several NoSQL database types you can use. I'd recommend you to use the Document Store database. I believe the most used Document Store database is MongoDB

您可以使用幾種NoSQL數據庫類型。我建議您使用文檔存儲數據庫。我相信最常用的文檔存儲數據庫是MongoDB

The example below stores JSON data into MongoDB.

下面的示例將JSON數據存儲到MongoDB中。

MongoDB and NodeJS tutorial

MongoDB和NodeJS教程

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/my_database_name';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    //HURRAY!! We are connected. :)
    console.log('Connection established to', url);

    // Get the documents collection
    var collection = db.collection('users');

    //Create some users
    var user1 = {name: 'modulus admin', age: 42, roles: ['admin', 'moderator', 'user']};
    var user2 = {name: 'modulus user', age: 22, roles: ['user']};
    var user3 = {name: 'modulus super admin', age: 92, roles: ['super-admin', 'admin', 'moderator', 'user']};

    // Insert some users
    collection.insert([user1, user2, user3], function (err, result) {
      if (err) {
        console.log(err);
      } else {
        console.log('Inserted %d documents into the "users" collection. The documents inserted with "_id" are:', result.length, result);
      }
      //Close connection
      db.close();
    });
  }
});

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/07/05/724df4798ac9a9b624e29759d7fc02a9.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com