从数据库中存储和查询JSON[英] Storing and querying JSON from a database

本文是小编为大家收集整理的关于从数据库中存储和查询JSON的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我听说过MongoDB,但我不确定我完全理解了这个概念.

如果我在mongodb中存储了多个JSON对象:

[{"id": "peter",
  "age": "12",
  "gender": "male"},
 {"id": "gemma",
  "age": "12",
  "gender": "female"},
 {"id": "paul",
  "age": "13",
  "gender": "male"}]

我将如何使用age >= 12?

查询所有JSON对象

推荐答案

首先,请理解JSON只是一种序列化技术.本身本身,这种序列化方法可能不应确定您的持久性媒介.从表面上看您的问题,似乎您正在寻找的是一个典型的关系存储数据库,您可以在其中使用SQL以灵活的方式查询数据.

从这种关系数据库中检索到回收后,在任何编程语言中序列化的JSON数据是为了存储到或进行介绍的,这是微不足道的.

现在,如果您确实需要存储各种没有真正具有固定结构的JSON文档(或任何其他类型的文档)的片段,那确实是您通常会开始查看NOSQL类型的解决方案(例如) mongodb.使用更流行的NOSQL数据库的另一个可能的情况是,当您处理大量数据并需要水平扩展时(即数据如此之大,需要跨多个服务器扩展数据库).许多NOSQL系统比传统的关系数据库更容易做到这一点.当然,在这种情况下,您需要根据它们提供的功能来评估这些工具,以允许您以最有用的方式读取,写入和查询数据.

其他推荐答案

从5.7开始,MySQL现在具有本机 json dataType/a>.假设您的表被称为students,并且JSON列称为student,在MySQL 5.7中,您的选择可以写为

SELECT * FROM students WHERE JSON_EXTRACT(student, '$.age') = 12;

其他推荐答案

mongoDB以BSON格式存储数据,与JSON相似.您可以以JSON格式存储数据,并且可以在任何字段上进行查询.您甚至可以在用于主要查询的特定字段上进行索引.

您不仅限于MongoDB,看到您的问题,我认为任何文档商店都适合您的需求.您在这里阅读更多信息: - http://en.wikipedia.org/wiki/wiki/document-oriented_database_database >

本文地址:https://www.itbaoku.cn/post/597454.html

问题描述

I've heard about MongoDB, but I'm not sure I fully understand the concept.

If I have multiple JSON objects stored in MongoDB:

[{"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?

推荐答案

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.

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.

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).

其他推荐答案

Starting from 5.7, MySQL now has native JSON datatype. Assuming your table is called students and the JSON column is called student, in MySQL 5.7 your select can be written as

SELECT * FROM students WHERE JSON_EXTRACT(student, '$.age') = 12;

其他推荐答案

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.

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