从数据库中存储和查询 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19189849/
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
Storing and querying JSON from a database
提问by Jakob
I've heard about MongoDB, but I'm not sure I fully understand the concept.
我听说过 MongoDB,但我不确定我是否完全理解这个概念。
If I have multiple JSON objects stored in MongoDB:
如果我在 MongoDB 中存储了多个 JSON 对象:
[{"id": "peter",
"age": "12",
"gender": "male"},
{"id": "gemma",
"age": "12",
"gender": "female"},
{"id": "paul",
"age": "13",
"gender": "male"}]
How would I be able to query all JSON objects with age >= 12?
我如何能够查询所有 JSON 对象age >= 12?
回答by Mike Brant
First of all, understand that JSON is just a serialization technique. In and of itself, this serialization method probably should not determine your persistence medium. Looking at your question on the surface, it seems like what you are looking for is a typical relational storage database where you can use SQL to query against your data in a flexible manner.
首先要明白JSON只是一种序列化技术。就其本身而言,这种序列化方法可能不应该确定您的持久性介质。从表面上看你的问题,看起来你正在寻找的是一个典型的关系存储数据库,你可以使用 SQL 以灵活的方式查询你的数据。
Serializing/deserializing JSON data for storage into or for presentation after retrieval from such a relational database is trivial in pretty much any programming language.
序列化/反序列化 JSON 数据以存储到这样的关系数据库中或在从这样的关系数据库中检索后进行展示在几乎任何编程语言中都是微不足道的。
Now if you truly need to store various snippets of JSON documents (or any other sort of document) that don't really have a fixed structure, that is really when you typically would start looking at a NoSQL type of solution such as MongoDB. One other possible such scenario for using the more popular NoSQL databases is when you are dealing with massive amounts of data and need to scale horizontally (i.e. the data is so large you need to scale the database across multiple servers). Many NoSQL systems make this much easier to do than traditional relational DB's. Of course in such a scenario, you would then need to evaluate those tools based on the functionality they provide in allowing you to read, write, and query data in the most useful manner for your use case(s).
现在,如果您确实需要存储实际上没有固定结构的 JSON 文档(或任何其他类型的文档)的各种片段,那么您通常会开始查看 NoSQL 类型的解决方案,例如 MongoDB。使用更流行的 NoSQL 数据库的另一种可能的情况是,当您处理大量数据并需要水平扩展时(即数据太大,您需要跨多个服务器扩展数据库)。许多 NoSQL 系统比传统的关系数据库更容易做到这一点。当然,在这种情况下,您需要根据这些工具提供的功能来评估这些工具,这些功能允许您以对您的用例最有用的方式读取、写入和查询数据。
回答by Sophia Feng
Starting from 5.7, MySQL now has native JSON datatype. Assuming your table is called studentsand the JSON column is called student, in MySQL 5.7 your select can be written as
从 5.7 开始,MySQL 现在具有原生JSON 数据类型。假设您的表被调用students并且 JSON 列被调用student,在 MySQL 5.7 中,您的选择可以写为
SELECT * FROM students WHERE JSON_EXTRACT(student, '$.age') = 12;
回答by Manu Viswam
MongoDB stores data in BSON format which is similar to JSON. You can store data in JSON format and can make a query on any field. You can even index on a particular field which is used for major queries.
MongoDB 以类似于 JSON 的 BSON 格式存储数据。您可以以 JSON 格式存储数据,并且可以对任何字段进行查询。您甚至可以对用于主要查询的特定字段进行索引。
You are not limited to MongoDB, Seeing your question i think any of the Document-store will suit your needs. You read more here :- http://en.wikipedia.org/wiki/Document-oriented_database
您不仅限于 MongoDB,看到您的问题,我认为任何文档存储都将满足您的需求。您在此处阅读更多信息:- http://en.wikipedia.org/wiki/Document-oriented_database
回答by tskuzzy
If all your objects have the same fields (id, age, gender, etc.) then you should store the objects as rows of some relational database (e.g. MySQL).
如果您的所有对象都具有相同的字段(id、age、gender 等),那么您应该将这些对象存储为某个关系数据库(例如 MySQL)的行。
回答by charles ross
A mongodb shell query for that might look like this:
一个 mongodb shell 查询可能如下所示:
db.people.find({"age": {gt:12 }});
回答by Keshav Murthy
A Couchbase N1QL query will look like this:
Couchbase N1QL 查询将如下所示:
SELECT * FROM people WHERE age >= 12;
See https://query.couchbase.comfor more details.
有关更多详细信息,请参阅https://query.couchbase.com。

