我们可以使用 JSON 作为数据库吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13899342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can we use JSON as a database?
提问by sami
I'm looking for fast and efficient data storage to build my PHP based web site. I'm aware of MySql. Can I use a JSON file in my server root directory instead of a MySQL database? If yes, what is the best way to do it?
我正在寻找快速有效的数据存储来构建我的基于 PHP 的网站。我知道MySql。我可以在我的服务器根目录中使用 JSON 文件而不是 MySQL 数据库吗?如果是,最好的方法是什么?
回答by hyde
You can use any single file, including a JSON file, like this:
您可以使用任何单个文件,包括 JSON 文件,如下所示:
Lock it somehow (google PHP file locking, it's possibly as simple as adding a parameter to file open function or changing function name to locking version).
Read the data from file and parse it to internal data stucture.
Optionally modify the data in internal data structure.
If you modified the data, truncate the file to 0 length and write new data to it.
Unlock the file as soon as you can, other requests may be waiting...
You can keep using the data in internal structures to render the page, just remember it may be out-dated as soon as you release the file lock and other HTTP request can modify it.
以某种方式锁定它(谷歌 PHP 文件锁定,它可能就像向文件打开函数添加参数或将函数名称更改为锁定版本一样简单)。
从文件中读取数据并将其解析为内部数据结构。
可选择修改内部数据结构中的数据。
如果您修改了数据,请将文件截断为 0 长度并将新数据写入其中。
尽快解锁文件,其他请求可能正在等待...
您可以继续使用内部结构中的数据来呈现页面,请记住,一旦您释放文件锁并且其他 HTTP 请求可以修改它,它可能会过时。
Also, if you modify the data from user's web form, remember that it may have been modified in between. Like, load page with user details for editing, then other user deletes that user, then editer tries to save the changed details, and should probably get error instead of re-creating deleted user.
此外,如果您修改了用户 Web 表单中的数据,请记住它可能已在两者之间进行了修改。例如,加载包含用户详细信息进行编辑的页面,然后其他用户删除该用户,然后编辑器尝试保存更改的详细信息,并且可能会出现错误而不是重新创建已删除的用户。
Note: This is very inefficient. If you are building a site where you expect more than say 10 simultaneous users, you have to use a more sophisticated scheme, or just use existing database... Also, you can't have too much data, because parsing JSON and generating modified JSON takes time.
注意:这是非常低效的。如果您正在构建一个站点,您希望同时有 10 个以上的用户,则必须使用更复杂的方案,或者仅使用现有数据库...此外,您不能拥有太多数据,因为解析 JSON 并生成修改的JSON 需要时间。
As long as you have just one user at a time, it'll just get slower and slower as amount of data grows, but as user count increases, and more users means both more requests andmore data, things start to get exponentiallyslower and you very soon hit limit where HTTP requests start to expire before file is available for handling the request...
只要你在某一时刻只有一个用户,它只会越来越慢作为的数据增长量,但是随着用户数量的增加,多的用户既指更多的要求和更多的数据,事情开始变得成倍速度较慢,在文件可用于处理请求之前,您很快就会达到 HTTP 请求开始过期的限制...
At that point, do not try to hack it to make it faster, but instead pick some existing database framework (SQL or nosql or file-based). If you start hacking together your own, you just end up re-inventing the wheel, usually poorly :-). Well, unless it is just programming exercise, but even then it might be better to instead learn use of some existing framework.
在这一点上,不要试图破解它以使其更快,而是选择一些现有的数据库框架(SQL 或 nosql 或基于文件的)。如果你开始自己动手,你最终只会重新发明轮子,通常很糟糕:-)。好吧,除非它只是编程练习,但即便如此,最好还是学习使用一些现有框架。
回答by Richard Burkhardt
回答by ceinmart
The new version of IBM Informix 12.10 xC2 supports now JSON.
check the link : http://pic.dhe.ibm.com/infocenter/informix/v121/topic/com.ibm.json.doc/ids_json_007.htm
IBM Informix 12.10 xC2 的新版本现在支持 JSON。
检查链接:http: //pic.dhe.ibm.com/infocenter/informix/v121/topic/com.ibm.json.doc/ids_json_007.htm
The manual says it is compatible with MongoDB drivers.
该手册说它与 MongoDB 驱动程序兼容。
About the Informix JSON compatibility
Applications that use the JSON-oriented query language, created by MongoDB, can interact with data stored in Informix? databases. The Informix database server also provides built-in JSON and BSON (binary JSON) data types.
You can use MongoDB community drivers to insert, update, and query JSON documents in Informix.
关于 Informix JSON 兼容性
使用 MongoDB 创建的面向 JSON 的查询语言的应用程序可以与存储在 Informix 中的数据进行交互吗?数据库。Informix 数据库服务器还提供内置的 JSON 和 BSON(二进制 JSON)数据类型。
您可以使用 MongoDB 社区驱动程序在 Informix 中插入、更新和查询 JSON 文档。
Not sure, but I believe you can use the Innovator-C edition (free for production) to test and use it with no-cost either for production enviroment.
不确定,但我相信您可以使用 Innovator-C 版本(免费用于生产)在生产环境中免费测试和使用它。

